diff --git a/homeassistant/components/openuv/__init__.py b/homeassistant/components/openuv/__init__.py index e1af166a3c2..72bdbbd179a 100644 --- a/homeassistant/components/openuv/__init__.py +++ b/homeassistant/components/openuv/__init__.py @@ -166,28 +166,16 @@ class OpenUV: class OpenUvEntity(Entity): """Define a generic OpenUV entity.""" - def __init__(self, openuv): + def __init__(self, openuv, sensor_type): """Initialize.""" - self._attrs = {ATTR_ATTRIBUTION: DEFAULT_ATTRIBUTION} - self._available = True - self._name = None + self._attr_extra_state_attributes = {ATTR_ATTRIBUTION: DEFAULT_ATTRIBUTION} + self._attr_should_poll = False + self._attr_unique_id = ( + f"{openuv.client.latitude}_{openuv.client.longitude}_{sensor_type}" + ) + self._sensor_type = sensor_type self.openuv = openuv - @property - def available(self) -> bool: - """Return True if entity is available.""" - return self._available - - @property - def extra_state_attributes(self): - """Return the state attributes.""" - return self._attrs - - @property - def name(self): - """Return the name of the entity.""" - return self._name - async def async_added_to_hass(self): """Register callbacks.""" diff --git a/homeassistant/components/openuv/binary_sensor.py b/homeassistant/components/openuv/binary_sensor.py index 62a83cdb141..eac67909b86 100644 --- a/homeassistant/components/openuv/binary_sensor.py +++ b/homeassistant/components/openuv/binary_sensor.py @@ -27,9 +27,7 @@ async def async_setup_entry(hass, entry, async_add_entities): binary_sensors = [] for kind, attrs in BINARY_SENSORS.items(): name, icon = attrs - binary_sensors.append( - OpenUvBinarySensor(openuv, kind, name, icon, entry.entry_id) - ) + binary_sensors.append(OpenUvBinarySensor(openuv, kind, name, icon)) async_add_entities(binary_sensors, True) @@ -37,38 +35,12 @@ async def async_setup_entry(hass, entry, async_add_entities): class OpenUvBinarySensor(OpenUvEntity, BinarySensorEntity): """Define a binary sensor for OpenUV.""" - def __init__(self, openuv, sensor_type, name, icon, entry_id): + def __init__(self, openuv, sensor_type, name, icon): """Initialize the sensor.""" - super().__init__(openuv) + super().__init__(openuv, sensor_type) - self._async_unsub_dispatcher_connect = None - self._entry_id = entry_id - self._icon = icon - self._latitude = openuv.client.latitude - self._longitude = openuv.client.longitude - self._name = name - self._sensor_type = sensor_type - self._state = None - - @property - def icon(self): - """Return the icon.""" - return self._icon - - @property - def is_on(self): - """Return the status of the sensor.""" - return self._state - - @property - def should_poll(self): - """Disable polling.""" - return False - - @property - def unique_id(self) -> str: - """Return a unique, Home Assistant friendly identifier for this entity.""" - return f"{self._latitude}_{self._longitude}_{self._sensor_type}" + self._attr_icon = icon + self._attr_name = name @callback def update_from_latest_data(self): @@ -76,10 +48,10 @@ class OpenUvBinarySensor(OpenUvEntity, BinarySensorEntity): data = self.openuv.data[DATA_PROTECTION_WINDOW] if not data: - self._available = False + self._attr_available = False return - self._available = True + self._attr_available = True for key in ("from_time", "to_time", "from_uv", "to_uv"): if not data.get(key): @@ -87,12 +59,12 @@ class OpenUvBinarySensor(OpenUvEntity, BinarySensorEntity): return if self._sensor_type == TYPE_PROTECTION_WINDOW: - self._state = ( + self._attr_is_on = ( parse_datetime(data["from_time"]) <= utcnow() <= parse_datetime(data["to_time"]) ) - self._attrs.update( + self._attr_extra_state_attributes.update( { ATTR_PROTECTION_WINDOW_ENDING_TIME: as_local( parse_datetime(data["to_time"]) diff --git a/homeassistant/components/openuv/sensor.py b/homeassistant/components/openuv/sensor.py index 654a89cfcf9..6f4e4e18d34 100644 --- a/homeassistant/components/openuv/sensor.py +++ b/homeassistant/components/openuv/sensor.py @@ -83,7 +83,7 @@ async def async_setup_entry(hass, entry, async_add_entities): sensors = [] for kind, attrs in SENSORS.items(): name, icon, unit = attrs - sensors.append(OpenUvSensor(openuv, kind, name, icon, unit, entry.entry_id)) + sensors.append(OpenUvSensor(openuv, kind, name, icon, unit)) async_add_entities(sensors, True) @@ -91,44 +91,13 @@ async def async_setup_entry(hass, entry, async_add_entities): class OpenUvSensor(OpenUvEntity, SensorEntity): """Define a binary sensor for OpenUV.""" - def __init__(self, openuv, sensor_type, name, icon, unit, entry_id): + def __init__(self, openuv, sensor_type, name, icon, unit): """Initialize the sensor.""" - super().__init__(openuv) + super().__init__(openuv, sensor_type) - self._async_unsub_dispatcher_connect = None - self._entry_id = entry_id - self._icon = icon - self._latitude = openuv.client.latitude - self._longitude = openuv.client.longitude - self._name = name - self._sensor_type = sensor_type - self._state = None - self._unit = unit - - @property - def icon(self): - """Return the icon.""" - return self._icon - - @property - def should_poll(self): - """Disable polling.""" - return False - - @property - def state(self): - """Return the status of the sensor.""" - return self._state - - @property - def unique_id(self) -> str: - """Return a unique, Home Assistant friendly identifier for this entity.""" - return f"{self._latitude}_{self._longitude}_{self._sensor_type}" - - @property - def unit_of_measurement(self): - """Return the unit the value is expressed in.""" - return self._unit + self._attr_icon = icon + self._attr_name = name + self._attr_unit_of_measurement = unit @callback def update_from_latest_data(self): @@ -136,29 +105,29 @@ class OpenUvSensor(OpenUvEntity, SensorEntity): data = self.openuv.data[DATA_UV].get("result") if not data: - self._available = False + self._attr_available = False return - self._available = True + self._attr_available = True if self._sensor_type == TYPE_CURRENT_OZONE_LEVEL: - self._state = data["ozone"] + self._attr_state = data["ozone"] elif self._sensor_type == TYPE_CURRENT_UV_INDEX: - self._state = data["uv"] + self._attr_state = data["uv"] elif self._sensor_type == TYPE_CURRENT_UV_LEVEL: if data["uv"] >= 11: - self._state = UV_LEVEL_EXTREME + self._attr_state = UV_LEVEL_EXTREME elif data["uv"] >= 8: - self._state = UV_LEVEL_VHIGH + self._attr_state = UV_LEVEL_VHIGH elif data["uv"] >= 6: - self._state = UV_LEVEL_HIGH + self._attr_state = UV_LEVEL_HIGH elif data["uv"] >= 3: - self._state = UV_LEVEL_MODERATE + self._attr_state = UV_LEVEL_MODERATE else: - self._state = UV_LEVEL_LOW + self._attr_state = UV_LEVEL_LOW elif self._sensor_type == TYPE_MAX_UV_INDEX: - self._state = data["uv_max"] - self._attrs.update( + self._attr_state = data["uv_max"] + self._attr_extra_state_attributes.update( {ATTR_MAX_UV_TIME: as_local(parse_datetime(data["uv_max_time"]))} ) elif self._sensor_type in ( @@ -169,6 +138,6 @@ class OpenUvSensor(OpenUvEntity, SensorEntity): TYPE_SAFE_EXPOSURE_TIME_5, TYPE_SAFE_EXPOSURE_TIME_6, ): - self._state = data["safe_exposure_time"][ + self._attr_state = data["safe_exposure_time"][ EXPOSURE_TYPE_MAP[self._sensor_type] ]