def test_temperature_to_metric(self): """Test temperature conversion to metric system.""" assert 25 == \ METRIC_SYSTEM.temperature(25, METRIC_SYSTEM.temperature_unit) assert 26.7 == \ round(METRIC_SYSTEM.temperature( 80, IMPERIAL_SYSTEM.temperature_unit), 1)
def test_temperature_to_metric(self): """Test temperature conversion to metric system.""" self.assertEqual( 25, METRIC_SYSTEM.temperature(25, METRIC_SYSTEM.temperature_unit)) self.assertEqual( 26.7, METRIC_SYSTEM.temperature(80, IMPERIAL_SYSTEM.temperature_unit))
def test_accumulated_precipitation_to_metric(): """Test accumulated_precipitation conversion to metric system.""" assert (METRIC_SYSTEM.accumulated_precipitation( 25, METRIC_SYSTEM.accumulated_precipitation_unit) == 25) assert METRIC_SYSTEM.accumulated_precipitation( 10, IMPERIAL_SYSTEM.accumulated_precipitation_unit) == pytest.approx( 254, abs=1e-4)
def test_temperature_to_metric(): """Test temperature conversion to metric system.""" assert METRIC_SYSTEM.temperature(25, METRIC_SYSTEM.temperature_unit) == 25 assert ( round(METRIC_SYSTEM.temperature(80, IMPERIAL_SYSTEM.temperature_unit), 1) == 26.7 )
def test_pressure_to_metric(self): """Test pressure conversion to metric system.""" assert 25 == \ METRIC_SYSTEM.pressure(25, METRIC_SYSTEM.pressure_unit) self.assertAlmostEqual( METRIC_SYSTEM.pressure(14.7, IMPERIAL_SYSTEM.pressure_unit), 101352.932, places=1)
def test_pressure_to_metric(self): """Test pressure conversion to metric system.""" assert 25 == \ METRIC_SYSTEM.pressure(25, METRIC_SYSTEM.pressure_unit) self.assertAlmostEqual(METRIC_SYSTEM.pressure( 14.7, IMPERIAL_SYSTEM.pressure_unit), 101352.932, places=1)
def test_length_to_metric(self): """Test length conversion to metric system.""" self.assertEqual( 100, METRIC_SYSTEM.length(100, METRIC_SYSTEM.length_unit) ) self.assertEqual( 8.04672, METRIC_SYSTEM.length(5, IMPERIAL_SYSTEM.length_unit) )
def test_config_as_dict(): """Test as dict.""" config = ha.Config(None) config.config_dir = "/test/ha-config" config.hass = MagicMock() type(config.hass.state).value = PropertyMock(return_value="RUNNING") expected = { "latitude": 0, "longitude": 0, "elevation": 0, CONF_UNIT_SYSTEM: METRIC_SYSTEM.as_dict(), "location_name": "Home", "time_zone": "UTC", "components": set(), "config_dir": "/test/ha-config", "whitelist_external_dirs": set(), "allowlist_external_dirs": set(), "allowlist_external_urls": set(), "version": __version__, "config_source": "default", "safe_mode": False, "state": "RUNNING", "external_url": None, "internal_url": None, } assert expected == config.as_dict()
def test_invalid_value(self): """Test no conversion happens if value is non-numeric.""" with pytest.raises(TypeError): METRIC_SYSTEM.length('25a', LENGTH_KILOMETERS) with pytest.raises(TypeError): METRIC_SYSTEM.temperature('50K', TEMP_CELSIUS) with pytest.raises(TypeError): METRIC_SYSTEM.volume('50L', VOLUME_LITERS) with pytest.raises(TypeError): METRIC_SYSTEM.pressure('50Pa', PRESSURE_PA)
def test_invalid_value(): """Test no conversion happens if value is non-numeric.""" with pytest.raises(TypeError): METRIC_SYSTEM.length("25a", LENGTH_KILOMETERS) with pytest.raises(TypeError): METRIC_SYSTEM.temperature("50K", TEMP_CELSIUS) with pytest.raises(TypeError): METRIC_SYSTEM.volume("50L", VOLUME_LITERS) with pytest.raises(TypeError): METRIC_SYSTEM.pressure("50Pa", PRESSURE_PA)
def test_as_dict(self): """Test that the as_dict() method returns the expected dictionary.""" expected = { LENGTH: LENGTH_KILOMETERS, TEMPERATURE: TEMP_CELSIUS, VOLUME: VOLUME_LITERS, MASS: MASS_GRAMS } self.assertEqual(expected, METRIC_SYSTEM.as_dict())
def test_as_dict(self): """Test that the as_dict() method returns the expected dictionary.""" expected = { LENGTH: LENGTH_KILOMETERS, TEMPERATURE: TEMP_CELSIUS, VOLUME: VOLUME_LITERS, MASS: MASS_GRAMS } assert expected == METRIC_SYSTEM.as_dict()
def test_as_dict(): """Test that the as_dict() method returns the expected dictionary.""" expected = { LENGTH: LENGTH_KILOMETERS, WIND_SPEED: SPEED_METERS_PER_SECOND, TEMPERATURE: TEMP_CELSIUS, VOLUME: VOLUME_LITERS, MASS: MASS_GRAMS, PRESSURE: PRESSURE_PA, ACCUMULATED_PRECIPITATION: LENGTH_MILLIMETERS, } assert expected == METRIC_SYSTEM.as_dict()
def test_as_dict(self): """Test as dict.""" expected = { 'latitude': None, 'longitude': None, CONF_UNIT_SYSTEM: METRIC_SYSTEM.as_dict(), 'location_name': None, 'time_zone': 'UTC', 'components': [], 'version': __version__, } self.assertEqual(expected, self.config.as_dict())
async def async_setup_entry(hass: HomeAssistant, config_entry: ConfigEntry) -> bool: """Set up the GeoNet NZ Quakes component as config entry.""" hass.data.setdefault(DOMAIN, {}) feeds = hass.data[DOMAIN].setdefault(FEED, {}) radius = config_entry.data[CONF_RADIUS] if hass.config.units.name == CONF_UNIT_SYSTEM_IMPERIAL: radius = METRIC_SYSTEM.length(radius, LENGTH_MILES) # Create feed entity manager for all platforms. manager = GeonetnzQuakesFeedEntityManager(hass, config_entry, radius) feeds[config_entry.entry_id] = manager _LOGGER.debug("Feed entity manager added for %s", config_entry.entry_id) await manager.async_init() return True
def set_temperature(self, **kwargs): mode = kwargs.get(ATTR_HVAC_MODE) if mode != None: self.set_hvac_mode(mode) temp = kwargs.get(ATTR_TEMPERATURE) if self._device.units.temperature_unit != TEMP_FAHRENHEIT: if self._device.force_fahrenheit: # undo HA's conversion temp = METRIC_SYSTEM.temperature(temp, TEMP_FAHRENHEIT) else: temp = IMPERIAL_SYSTEM.temperature( temp, self._device.units.temperature_unit) self._device.dps(DPS_TARGET, int(temp + 0.5))
async def async_setup_entry(hass, config_entry): """Set up the GeoNet NZ Volcano component as config entry.""" hass.data.setdefault(DOMAIN, {}) hass.data[DOMAIN].setdefault(FEED, {}) radius = config_entry.data[CONF_RADIUS] unit_system = config_entry.data[CONF_UNIT_SYSTEM] if unit_system == CONF_UNIT_SYSTEM_IMPERIAL: radius = METRIC_SYSTEM.length(radius, LENGTH_MILES) # Create feed entity manager for all platforms. manager = GeonetnzVolcanoFeedEntityManager(hass, config_entry, radius, unit_system) hass.data[DOMAIN][FEED][config_entry.entry_id] = manager _LOGGER.debug("Feed entity manager added for %s", config_entry.entry_id) await manager.async_init() return True
def test_as_dict(self): """Test as dict.""" self.config.config_dir = '/tmp/ha-config' expected = { 'latitude': None, 'longitude': None, 'elevation': None, CONF_UNIT_SYSTEM: METRIC_SYSTEM.as_dict(), 'location_name': None, 'time_zone': 'UTC', 'components': set(), 'config_dir': '/tmp/ha-config', 'whitelist_external_dirs': set(), 'version': __version__, } assert expected == self.config.as_dict()
def test_as_dict(self): """Test as dict.""" self.config.config_dir = "/test/ha-config" expected = { "latitude": 0, "longitude": 0, "elevation": 0, CONF_UNIT_SYSTEM: METRIC_SYSTEM.as_dict(), "location_name": "Home", "time_zone": "UTC", "components": set(), "config_dir": "/test/ha-config", "whitelist_external_dirs": set(), "version": __version__, "config_source": "default", } assert expected == self.config.as_dict()
def test_pressure_same_unit(): """Test no conversion happens if to unit is same as from unit.""" assert METRIC_SYSTEM.pressure(5, METRIC_SYSTEM.pressure_unit) == 5
def test_length_to_metric(): """Test length conversion to metric system.""" assert METRIC_SYSTEM.length(100, METRIC_SYSTEM.length_unit) == 100 assert METRIC_SYSTEM.length(5, IMPERIAL_SYSTEM.length_unit) == 8.04672
def test_length_unknown_unit(): """Test length conversion with unknown from unit.""" with pytest.raises(ValueError): METRIC_SYSTEM.length(5, "fr")
def test_length_to_metric(self): """Test length conversion to metric system.""" self.assertEqual(100, METRIC_SYSTEM.length(100, METRIC_SYSTEM.length_unit)) self.assertEqual(8.04672, METRIC_SYSTEM.length(5, IMPERIAL_SYSTEM.length_unit))
def test_length_to_metric(self): """Test length conversion to metric system.""" assert 100 == \ METRIC_SYSTEM.length(100, METRIC_SYSTEM.length_unit) assert 8.04672 == \ METRIC_SYSTEM.length(5, IMPERIAL_SYSTEM.length_unit)
def test_length_unknown_unit(self): """Test length conversion with unknown from unit.""" with self.assertRaises(ValueError): METRIC_SYSTEM.length(5, 'fr')
def test_accumulated_precipitation_same_unit(): """Test no conversion happens if to unit is same as from unit.""" assert (METRIC_SYSTEM.accumulated_precipitation( 5, METRIC_SYSTEM.accumulated_precipitation_unit) == 5)
def test_pressure_unknown_unit(self): """Test no conversion happens if unknown unit.""" with pytest.raises(ValueError): METRIC_SYSTEM.pressure(5, 'K')
def test_invalid_value(self): """Test no conversion happens if value is non-numeric.""" with pytest.raises(TypeError): METRIC_SYSTEM.length('25a', LENGTH_KILOMETERS) with pytest.raises(TypeError): METRIC_SYSTEM.temperature('50K', TEMP_CELSIUS)
def test_invalid_value(self): """Test no conversion happens if value is non-numeric.""" with self.assertRaises(TypeError): METRIC_SYSTEM.length('25a', LENGTH_KILOMETERS) with self.assertRaises(TypeError): METRIC_SYSTEM.temperature('50K', TEMP_CELSIUS)
def test_accumulated_precipitation_unknown_unit(): """Test no conversion happens if unknown unit.""" with pytest.raises(ValueError): METRIC_SYSTEM.accumulated_precipitation(5, "K")
def test_pressure_unknown_unit(): """Test no conversion happens if unknown unit.""" with pytest.raises(ValueError): METRIC_SYSTEM.pressure(5, "K")
def test_pressure_to_metric(): """Test pressure conversion to metric system.""" assert METRIC_SYSTEM.pressure(25, METRIC_SYSTEM.pressure_unit) == 25 assert METRIC_SYSTEM.pressure( 14.7, IMPERIAL_SYSTEM.pressure_unit) == pytest.approx(101352.932, abs=1e-1)
def celsius(deg: Optional[float]) -> Optional[float]: """Convert a float to Celsius and rounds to one decimal place.""" if deg is None: return None return round(METRIC_SYSTEM.temperature(deg, units.temperature_unit), 1)
def test_temperature_unknown_unit(self): """Test no conversion happens if unknown unit.""" with self.assertRaises(ValueError): METRIC_SYSTEM.temperature(5, 'K')
def test_temperature_unknown_unit(): """Test no conversion happens if unknown unit.""" with pytest.raises(ValueError): METRIC_SYSTEM.temperature(5, "abc")
def test_temperature_same_unit(self): """Test no conversion happens if to unit is same as from unit.""" self.assertEqual( 5, METRIC_SYSTEM.temperature(5, METRIC_SYSTEM.temperature_unit))
def test_temperature_same_unit(self): """Test no conversion happens if to unit is same as from unit.""" assert 5 == \ METRIC_SYSTEM.temperature(5, METRIC_SYSTEM.temperature_unit)
def test_pressure_same_unit(self): """Test no conversion happens if to unit is same as from unit.""" assert 5 == \ METRIC_SYSTEM.pressure(5, METRIC_SYSTEM.pressure_unit)