Remove redundant property definitions in Ambient PWS (#52350)

This commit is contained in:
Aaron Bach 2021-06-30 17:30:54 -05:00 committed by GitHub
parent b4e550dee2
commit cb2b6f5ff4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 42 additions and 85 deletions

View File

@ -493,62 +493,17 @@ class AmbientWeatherEntity(Entity):
): ):
"""Initialize the sensor.""" """Initialize the sensor."""
self._ambient = ambient self._ambient = ambient
self._device_class = device_class self._attr_device_class = device_class
self._mac_address = mac_address self._attr_device_info = {
self._sensor_name = sensor_name "identifiers": {(DOMAIN, mac_address)},
self._sensor_type = sensor_type "name": station_name,
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,
"manufacturer": "Ambient Weather", "manufacturer": "Ambient Weather",
} }
self._attr_name = f"{station_name}_{sensor_name}"
@property self._attr_should_poll = False
def name(self): self._attr_unique_id = f"{mac_address}_{sensor_type}"
"""Return the name of the sensor.""" self._mac_address = mac_address
return f"{self._station_name}_{self._sensor_name}" self._sensor_type = sensor_type
@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}"
async def async_added_to_hass(self): async def async_added_to_hass(self):
"""Register callbacks.""" """Register callbacks."""
@ -556,6 +511,21 @@ class AmbientWeatherEntity(Entity):
@callback @callback
def update(): def update():
"""Update the state.""" """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.update_from_latest_data()
self.async_write_ha_state() self.async_write_ha_state()

View File

@ -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): class AmbientWeatherBinarySensor(AmbientWeatherEntity, BinarySensorEntity):
"""Define an Ambient binary sensor.""" """Define an Ambient binary sensor."""
@property @callback
def is_on(self): def update_from_latest_data(self):
"""Return the status of the sensor.""" """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 ( if self._sensor_type in (
TYPE_BATT1, TYPE_BATT1,
TYPE_BATT10, TYPE_BATT10,
@ -72,13 +76,6 @@ class AmbientWeatherBinarySensor(AmbientWeatherEntity, BinarySensorEntity):
TYPE_PM25_BATT, TYPE_PM25_BATT,
TYPE_PM25IN_BATT, TYPE_PM25IN_BATT,
): ):
return self._state == 0 self._attr_is_on = state == 0
else:
return self._state == 1 self._attr_is_on = 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
)

View File

@ -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): class AmbientWeatherSensor(AmbientWeatherEntity, SensorEntity):
@ -54,17 +54,7 @@ class AmbientWeatherSensor(AmbientWeatherEntity, SensorEntity):
ambient, mac_address, station_name, sensor_type, sensor_name, device_class ambient, mac_address, station_name, sensor_type, sensor_name, device_class
) )
self._unit = unit self._attr_unit_of_measurement = 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
@callback @callback
def update_from_latest_data(self): def update_from_latest_data(self):
@ -78,10 +68,10 @@ class AmbientWeatherSensor(AmbientWeatherEntity, SensorEntity):
].get(TYPE_SOLARRADIATION) ].get(TYPE_SOLARRADIATION)
if w_m2_brightness_val is None: if w_m2_brightness_val is None:
self._state = None self._attr_state = None
else: else:
self._state = round(float(w_m2_brightness_val) / 0.0079) self._attr_state = round(float(w_m2_brightness_val) / 0.0079)
else: else:
self._state = self._ambient.stations[self._mac_address][ATTR_LAST_DATA].get( self._attr_state = self._ambient.stations[self._mac_address][
self._sensor_type ATTR_LAST_DATA
) ].get(self._sensor_type)