Example #1
0
def test_convert_invalid_unit():
    """Test exception is thrown for invalid units."""
    with pytest.raises(ValueError):
        pressure_util.convert(5, INVALID_SYMBOL, VALID_SYMBOL)

    with pytest.raises(ValueError):
        pressure_util.convert(5, VALID_SYMBOL, INVALID_SYMBOL)
    def test_convert_invalid_unit(self):
        """Test exception is thrown for invalid units."""
        with pytest.raises(ValueError):
            pressure_util.convert(5, INVALID_SYMBOL, VALID_SYMBOL)

        with pytest.raises(ValueError):
            pressure_util.convert(5, VALID_SYMBOL, INVALID_SYMBOL)
Example #3
0
def test_convert_same_unit():
    """Test conversion from any unit to same unit."""
    assert pressure_util.convert(2, PRESSURE_PA, PRESSURE_PA) == 2
    assert pressure_util.convert(3, PRESSURE_HPA, PRESSURE_HPA) == 3
    assert pressure_util.convert(4, PRESSURE_MBAR, PRESSURE_MBAR) == 4
    assert pressure_util.convert(5, PRESSURE_INHG, PRESSURE_INHG) == 5
    assert pressure_util.convert(6, PRESSURE_KPA, PRESSURE_KPA) == 6
Example #4
0
def test_convert_from_inhg():
    """Test conversion from inHg to other units."""
    inhg = 30
    assert pressure_util.convert(inhg, PRESSURE_INHG,
                                 PRESSURE_PSI) == pytest.approx(14.7346266155)
    assert pressure_util.convert(inhg, PRESSURE_INHG,
                                 PRESSURE_HPA) == pytest.approx(1015.9167)
    assert pressure_util.convert(inhg, PRESSURE_INHG,
                                 PRESSURE_PA) == pytest.approx(101591.67)
    assert pressure_util.convert(inhg, PRESSURE_INHG,
                                 PRESSURE_MBAR) == pytest.approx(1015.9167)
Example #5
0
def test_convert_from_hpascals():
    """Test conversion from hPA to other units."""
    hpascals = 1000
    assert pressure_util.convert(hpascals, PRESSURE_HPA,
                                 PRESSURE_PSI) == pytest.approx(14.5037743897)
    assert pressure_util.convert(hpascals, PRESSURE_HPA,
                                 PRESSURE_INHG) == pytest.approx(29.5299801647)
    assert pressure_util.convert(hpascals, PRESSURE_HPA,
                                 PRESSURE_PA) == pytest.approx(100000)
    assert pressure_util.convert(hpascals, PRESSURE_HPA,
                                 PRESSURE_MBAR) == pytest.approx(1000)
Example #6
0
 def test_convert_from_hpascals(self):
     """Test conversion from hPA to other units."""
     hpascals = 1000
     self.assertAlmostEqual(
         pressure_util.convert(hpascals, PRESSURE_HPA, PRESSURE_PSI),
         14.5037743897)
     self.assertAlmostEqual(
         pressure_util.convert(hpascals, PRESSURE_HPA, PRESSURE_INHG),
         29.5299801647)
     self.assertAlmostEqual(
         pressure_util.convert(hpascals, PRESSURE_HPA, PRESSURE_PA), 100000)
     self.assertAlmostEqual(
         pressure_util.convert(hpascals, PRESSURE_HPA, PRESSURE_MBAR), 1000)
Example #7
0
 def test_convert_from_inhg(self):
     """Test conversion from inHg to other units."""
     inhg = 30
     self.assertAlmostEqual(
         pressure_util.convert(inhg, PRESSURE_INHG, PRESSURE_PSI),
         14.7346266155)
     self.assertAlmostEqual(
         pressure_util.convert(inhg, PRESSURE_INHG, PRESSURE_HPA),
         1015.9167)
     self.assertAlmostEqual(
         pressure_util.convert(inhg, PRESSURE_INHG, PRESSURE_PA), 101591.67)
     self.assertAlmostEqual(
         pressure_util.convert(inhg, PRESSURE_INHG, PRESSURE_MBAR),
         1015.9167)
 def test_convert_from_inhg(self):
     """Test conversion from inHg to other units."""
     inhg = 30
     self.assertAlmostEqual(
         pressure_util.convert(inhg, PRESSURE_INHG, PRESSURE_PSI),
         14.7346266155)
     self.assertAlmostEqual(
         pressure_util.convert(inhg, PRESSURE_INHG, PRESSURE_HPA),
         1015.9167)
     self.assertAlmostEqual(
         pressure_util.convert(inhg, PRESSURE_INHG, PRESSURE_PA),
         101591.67)
     self.assertAlmostEqual(
         pressure_util.convert(inhg, PRESSURE_INHG, PRESSURE_MBAR),
         1015.9167)
 def test_convert_from_hpascals(self):
     """Test conversion from hPA to other units."""
     hpascals = 1000
     self.assertAlmostEqual(
         pressure_util.convert(hpascals, PRESSURE_HPA, PRESSURE_PSI),
         14.5037743897)
     self.assertAlmostEqual(
         pressure_util.convert(hpascals, PRESSURE_HPA, PRESSURE_INHG),
         29.5299801647)
     self.assertAlmostEqual(
         pressure_util.convert(hpascals, PRESSURE_HPA, PRESSURE_PA),
         100000)
     self.assertAlmostEqual(
         pressure_util.convert(hpascals, PRESSURE_HPA, PRESSURE_MBAR),
         1000)
Example #10
0
    def extra_state_attributes(self):
        s = self._vehicle.status
        attrs = {}

        # Statuses
        for k, v in DATA_ATTRS_TYRE_STATUS.items():
            if s.get(v):
                attrs[k.title() + " Status"] = s.get(v).title()

        # Hass lacks proper pressure conversions/units definitions.
        # So need to deal with here
        # Pressures
        for k, v in DATA_ATTRS_TYRE_PRESSURE.items():

            if s.get(v):
                tyre_pressure = int(s.get(v))
                # Some vehicles send in kPa*10, others in kPa. Ensure in kPa
                if tyre_pressure > 1000:
                    tyre_pressure = tyre_pressure / 10

                # Convert to local units - metric = bar, imperial = psi
                if self._units == PRESSURE_BAR:
                    attrs[k.title() +
                          " Pressure ({})".format(self._units)] = round(
                              tyre_pressure / 100, 2)
                else:
                    attrs[k.title() +
                          " Pressure ({})".format(self._units)] = round(
                              pressure.convert(tyre_pressure * 1000,
                                               PRESSURE_PA, self._units),
                              1,
                          )

        return attrs
Example #11
0
    def pressure(self, pressure: Optional[float], from_unit: str) -> float:
        """Convert the given pressure to this unit system."""
        if not isinstance(pressure, Number):
            raise TypeError('{} is not a numeric value.'.format(str(pressure)))

        return pressure_util.convert(pressure, from_unit,
                                     self.pressure_unit)
Example #12
0
    def pressure(self, pressure: float | None, from_unit: str) -> float:
        """Convert the given pressure to this unit system."""
        if not isinstance(pressure, Number):
            raise TypeError(f"{pressure!s} is not a numeric value.")

        # type ignore: https://github.com/python/mypy/issues/7207
        return pressure_util.convert(  # type: ignore
            pressure, from_unit, self.pressure_unit)
Example #13
0
def test_convert_from_mmhg():
    """Test conversion from mmHg to other units."""
    inhg = 30
    assert pressure_util.convert(inhg, PRESSURE_MMHG,
                                 PRESSURE_PSI) == pytest.approx(0.580102)
    assert pressure_util.convert(inhg, PRESSURE_MMHG,
                                 PRESSURE_KPA) == pytest.approx(3.99966)
    assert pressure_util.convert(inhg, PRESSURE_MMHG,
                                 PRESSURE_HPA) == pytest.approx(39.9966)
    assert pressure_util.convert(inhg, PRESSURE_MMHG,
                                 PRESSURE_PA) == pytest.approx(3999.66)
    assert pressure_util.convert(inhg, PRESSURE_MMHG,
                                 PRESSURE_MBAR) == pytest.approx(39.9966)
    assert pressure_util.convert(inhg, PRESSURE_MMHG,
                                 PRESSURE_CBAR) == pytest.approx(3.99966)
    assert pressure_util.convert(inhg, PRESSURE_MMHG,
                                 PRESSURE_INHG) == pytest.approx(1.181099)
Example #14
0
def test_convert_nonnumeric_value():
    """Test exception is thrown for nonnumeric type."""
    with pytest.raises(TypeError):
        pressure_util.convert("a", PRESSURE_HPA, PRESSURE_INHG)
Example #15
0
]

QUERY_STATISTIC_META_ID = [
    StatisticsMeta.id,
    StatisticsMeta.statistic_id,
]

STATISTICS_BAKERY = "recorder_statistics_bakery"
STATISTICS_META_BAKERY = "recorder_statistics_meta_bakery"
STATISTICS_SHORT_TERM_BAKERY = "recorder_statistics_short_term_bakery"

# Convert pressure and temperature statistics from the native unit used for statistics
# to the units configured by the user
UNIT_CONVERSIONS = {
    PRESSURE_PA:
    lambda x, units: pressure_util.convert(x, PRESSURE_PA, units.pressure_unit)
    if x is not None else None,
    TEMP_CELSIUS:
    lambda x, units: temperature_util.convert(
        x, TEMP_CELSIUS, units.temperature_unit) if x is not None else None,
    VOLUME_CUBIC_METERS:
    lambda x, units: volume_util.convert(
        x, VOLUME_CUBIC_METERS, _configured_unit(VOLUME_CUBIC_METERS, units))
    if x is not None else None,
}

_LOGGER = logging.getLogger(__name__)


def split_statistic_id(entity_id: str) -> list[str]:
    """Split a state entity ID into domain and object ID."""
Example #16
0
 def test_convert_nonnumeric_value(self):
     """Test exception is thrown for nonnumeric type."""
     with pytest.raises(TypeError):
         pressure_util.convert('a', PRESSURE_HPA, PRESSURE_INHG)
Example #17
0
 def test_convert_same_unit(self):
     """Test conversion from any unit to same unit."""
     assert pressure_util.convert(2, PRESSURE_PA, PRESSURE_PA) == 2
     assert pressure_util.convert(3, PRESSURE_HPA, PRESSURE_HPA) == 3
     assert pressure_util.convert(4, PRESSURE_MBAR, PRESSURE_MBAR) == 4
     assert pressure_util.convert(5, PRESSURE_INHG, PRESSURE_INHG) == 5