From 4a94ed8f5a2b99da7b771bf740e5dc67349fc8b1 Mon Sep 17 00:00:00 2001 From: Yuval Aboulafia Date: Wed, 30 Jun 2021 22:24:19 +0300 Subject: [PATCH] Use attrs instead of property for Jewish Calendar (#52333) * Use attrs instead of property for Jewish Calendar * clean time sensor class * move type and prefix up * revert is_on attribute --- .../jewish_calendar/binary_sensor.py | 30 ++++-------------- .../components/jewish_calendar/sensor.py | 31 +++++-------------- 2 files changed, 13 insertions(+), 48 deletions(-) diff --git a/homeassistant/components/jewish_calendar/binary_sensor.py b/homeassistant/components/jewish_calendar/binary_sensor.py index bda2bd5a117..954b22debd0 100644 --- a/homeassistant/components/jewish_calendar/binary_sensor.py +++ b/homeassistant/components/jewish_calendar/binary_sensor.py @@ -29,41 +29,23 @@ class JewishCalendarBinarySensor(BinarySensorEntity): def __init__(self, data, sensor, sensor_info): """Initialize the binary sensor.""" - self._location = data["location"] self._type = sensor - self._name = f"{data['name']} {sensor_info[0]}" - self._icon = sensor_info[1] + self._prefix = data["prefix"] + self._attr_name = f"{data['name']} {sensor_info[0]}" + self._attr_unique_id = f"{self._prefix}_{self._type}" + self._attr_icon = sensor_info[1] + self._attr_should_poll = False + self._location = data["location"] self._hebrew = data["language"] == "hebrew" self._candle_lighting_offset = data["candle_lighting_offset"] self._havdalah_offset = data["havdalah_offset"] - self._prefix = data["prefix"] self._update_unsub = None - @property - def icon(self): - """Return the icon of the entity.""" - return self._icon - - @property - def unique_id(self) -> str: - """Generate a unique id.""" - return f"{self._prefix}_{self._type}" - - @property - def name(self): - """Return the name of the entity.""" - return self._name - @property def is_on(self): """Return true if sensor is on.""" return self._get_zmanim().issur_melacha_in_effect - @property - def should_poll(self): - """No polling needed.""" - return False - def _get_zmanim(self): """Return the Zmanim object for now().""" return hdate.Zmanim( diff --git a/homeassistant/components/jewish_calendar/sensor.py b/homeassistant/components/jewish_calendar/sensor.py index 5690cd35a03..0dfc61970ef 100644 --- a/homeassistant/components/jewish_calendar/sensor.py +++ b/homeassistant/components/jewish_calendar/sensor.py @@ -35,33 +35,19 @@ class JewishCalendarSensor(SensorEntity): def __init__(self, data, sensor, sensor_info): """Initialize the Jewish calendar sensor.""" - self._location = data["location"] self._type = sensor - self._name = f"{data['name']} {sensor_info[0]}" - self._icon = sensor_info[1] + self._prefix = data["prefix"] + self._attr_name = f"{data['name']} {sensor_info[0]}" + self._attr_unique_id = f"{self._prefix}_{self._type}" + self._attr_icon = sensor_info[1] + self._location = data["location"] self._hebrew = data["language"] == "hebrew" self._candle_lighting_offset = data["candle_lighting_offset"] self._havdalah_offset = data["havdalah_offset"] self._diaspora = data["diaspora"] self._state = None - self._prefix = data["prefix"] self._holiday_attrs = {} - @property - def name(self): - """Return the name of the sensor.""" - return self._name - - @property - def unique_id(self) -> str: - """Generate a unique id.""" - return f"{self._prefix}_{self._type}" - - @property - def icon(self): - """Icon to display in the front end.""" - return self._icon - @property def state(self): """Return the state of the sensor.""" @@ -142,16 +128,13 @@ class JewishCalendarSensor(SensorEntity): class JewishCalendarTimeSensor(JewishCalendarSensor): """Implement attrbutes for sensors returning times.""" + _attr_device_class = DEVICE_CLASS_TIMESTAMP + @property def state(self): """Return the state of the sensor.""" return dt_util.as_utc(self._state) if self._state is not None else None - @property - def device_class(self): - """Return the class of this sensor.""" - return DEVICE_CLASS_TIMESTAMP - @property def extra_state_attributes(self): """Return the state attributes."""