From cb2b6f5ff4950b758faf19341d0cd23ce750dfbe Mon Sep 17 00:00:00 2001 From: Aaron Bach Date: Wed, 30 Jun 2021 17:30:54 -0500 Subject: [PATCH] Remove redundant property definitions in Ambient PWS (#52350) --- .../components/ambient_station/__init__.py | 78 ++++++------------- .../ambient_station/binary_sensor.py | 25 +++--- .../components/ambient_station/sensor.py | 24 ++---- 3 files changed, 42 insertions(+), 85 deletions(-) diff --git a/homeassistant/components/ambient_station/__init__.py b/homeassistant/components/ambient_station/__init__.py index 9036a4d89a2..347ba700060 100644 --- a/homeassistant/components/ambient_station/__init__.py +++ b/homeassistant/components/ambient_station/__init__.py @@ -493,62 +493,17 @@ class AmbientWeatherEntity(Entity): ): """Initialize the sensor.""" self._ambient = ambient - self._device_class = device_class - self._mac_address = mac_address - self._sensor_name = sensor_name - self._sensor_type = sensor_type - self._state = None - self._station_name = station_name - - @property - def available(self): - """Return True if entity is available.""" - # Since the solarradiation_lx sensor is created only if the - # user shows a solarradiation sensor, ensure that the - # solarradiation_lx sensor shows as available if the solarradiation - # sensor is available: - if self._sensor_type == TYPE_SOLARRADIATION_LX: - return ( - self._ambient.stations[self._mac_address][ATTR_LAST_DATA].get( - TYPE_SOLARRADIATION - ) - is not None - ) - return ( - self._ambient.stations[self._mac_address][ATTR_LAST_DATA].get( - self._sensor_type - ) - is not None - ) - - @property - def device_class(self): - """Return the device class.""" - return self._device_class - - @property - def device_info(self): - """Return device registry information for this entity.""" - return { - "identifiers": {(DOMAIN, self._mac_address)}, - "name": self._station_name, + self._attr_device_class = device_class + self._attr_device_info = { + "identifiers": {(DOMAIN, mac_address)}, + "name": station_name, "manufacturer": "Ambient Weather", } - - @property - def name(self): - """Return the name of the sensor.""" - return f"{self._station_name}_{self._sensor_name}" - - @property - def should_poll(self): - """Disable polling.""" - return False - - @property - def unique_id(self): - """Return a unique, unchanging string that represents this sensor.""" - return f"{self._mac_address}_{self._sensor_type}" + self._attr_name = f"{station_name}_{sensor_name}" + self._attr_should_poll = False + self._attr_unique_id = f"{mac_address}_{sensor_type}" + self._mac_address = mac_address + self._sensor_type = sensor_type async def async_added_to_hass(self): """Register callbacks.""" @@ -556,6 +511,21 @@ class AmbientWeatherEntity(Entity): @callback def update(): """Update the state.""" + if self._sensor_type == TYPE_SOLARRADIATION_LX: + self._attr_available = ( + self._ambient.stations[self._mac_address][ATTR_LAST_DATA].get( + TYPE_SOLARRADIATION + ) + is not None + ) + else: + self._attr_available = ( + self._ambient.stations[self._mac_address][ATTR_LAST_DATA].get( + self._sensor_type + ) + is not None + ) + self.update_from_latest_data() self.async_write_ha_state() diff --git a/homeassistant/components/ambient_station/binary_sensor.py b/homeassistant/components/ambient_station/binary_sensor.py index c2e5ad8b4f4..872b2f4b9fd 100644 --- a/homeassistant/components/ambient_station/binary_sensor.py +++ b/homeassistant/components/ambient_station/binary_sensor.py @@ -47,15 +47,19 @@ async def async_setup_entry(hass, entry, async_add_entities): ) ) - async_add_entities(binary_sensor_list, True) + async_add_entities(binary_sensor_list) class AmbientWeatherBinarySensor(AmbientWeatherEntity, BinarySensorEntity): """Define an Ambient binary sensor.""" - @property - def is_on(self): - """Return the status of the sensor.""" + @callback + def update_from_latest_data(self): + """Fetch new state data for the entity.""" + state = self._ambient.stations[self._mac_address][ATTR_LAST_DATA].get( + self._sensor_type + ) + if self._sensor_type in ( TYPE_BATT1, TYPE_BATT10, @@ -72,13 +76,6 @@ class AmbientWeatherBinarySensor(AmbientWeatherEntity, BinarySensorEntity): TYPE_PM25_BATT, TYPE_PM25IN_BATT, ): - return self._state == 0 - - return self._state == 1 - - @callback - def update_from_latest_data(self): - """Fetch new state data for the entity.""" - self._state = self._ambient.stations[self._mac_address][ATTR_LAST_DATA].get( - self._sensor_type - ) + self._attr_is_on = state == 0 + else: + self._attr_is_on = state == 1 diff --git a/homeassistant/components/ambient_station/sensor.py b/homeassistant/components/ambient_station/sensor.py index 7c60d1da9bc..69d41c035d0 100644 --- a/homeassistant/components/ambient_station/sensor.py +++ b/homeassistant/components/ambient_station/sensor.py @@ -33,7 +33,7 @@ async def async_setup_entry(hass, entry, async_add_entities): ) ) - async_add_entities(sensor_list, True) + async_add_entities(sensor_list) class AmbientWeatherSensor(AmbientWeatherEntity, SensorEntity): @@ -54,17 +54,7 @@ class AmbientWeatherSensor(AmbientWeatherEntity, SensorEntity): ambient, mac_address, station_name, sensor_type, sensor_name, device_class ) - self._unit = unit - - @property - def state(self): - """Return the state of the sensor.""" - return self._state - - @property - def unit_of_measurement(self): - """Return the unit of measurement.""" - return self._unit + self._attr_unit_of_measurement = unit @callback def update_from_latest_data(self): @@ -78,10 +68,10 @@ class AmbientWeatherSensor(AmbientWeatherEntity, SensorEntity): ].get(TYPE_SOLARRADIATION) if w_m2_brightness_val is None: - self._state = None + self._attr_state = None else: - self._state = round(float(w_m2_brightness_val) / 0.0079) + self._attr_state = round(float(w_m2_brightness_val) / 0.0079) else: - self._state = self._ambient.stations[self._mac_address][ATTR_LAST_DATA].get( - self._sensor_type - ) + self._attr_state = self._ambient.stations[self._mac_address][ + ATTR_LAST_DATA + ].get(self._sensor_type)