Use shorthand attributes in Smappee (#99837)

This commit is contained in:
Joost Lekkerkerker 2023-09-14 09:25:21 +02:00 committed by GitHub
parent 6692a37f0d
commit 923d945267
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 61 additions and 115 deletions

View File

@ -15,6 +15,23 @@ from .const import DOMAIN
BINARY_SENSOR_PREFIX = "Appliance"
PRESENCE_PREFIX = "Presence"
ICON_MAPPING = {
"Car Charger": "mdi:car",
"Coffeemaker": "mdi:coffee",
"Clothes Dryer": "mdi:tumble-dryer",
"Clothes Iron": "mdi:hanger",
"Dishwasher": "mdi:dishwasher",
"Lights": "mdi:lightbulb",
"Fan": "mdi:fan",
"Freezer": "mdi:fridge",
"Microwave": "mdi:microwave",
"Oven": "mdi:stove",
"Refrigerator": "mdi:fridge",
"Stove": "mdi:stove",
"Washing Machine": "mdi:washing-machine",
"Water Pump": "mdi:water-pump",
}
async def async_setup_entry(
hass: HomeAssistant,
@ -48,54 +65,33 @@ async def async_setup_entry(
class SmappeePresence(BinarySensorEntity):
"""Implementation of a Smappee presence binary sensor."""
_attr_device_class = BinarySensorDeviceClass.PRESENCE
def __init__(self, smappee_base, service_location):
"""Initialize the Smappee sensor."""
self._smappee_base = smappee_base
self._service_location = service_location
self._state = self._service_location.is_present
@property
def name(self):
"""Return the name of the binary sensor."""
return f"{self._service_location.service_location_name} - {PRESENCE_PREFIX}"
@property
def is_on(self):
"""Return if the binary sensor is turned on."""
return self._state
@property
def device_class(self):
"""Return the class of this device, from component DEVICE_CLASSES."""
return BinarySensorDeviceClass.PRESENCE
@property
def unique_id(
self,
):
"""Return the unique ID for this binary sensor."""
return (
f"{self._service_location.device_serial_number}-"
f"{self._service_location.service_location_id}-"
self._attr_name = (
f"{service_location.service_location_name} - {PRESENCE_PREFIX}"
)
self._attr_unique_id = (
f"{service_location.device_serial_number}-"
f"{service_location.service_location_id}-"
f"{BinarySensorDeviceClass.PRESENCE}"
)
@property
def device_info(self) -> DeviceInfo:
"""Return the device info for this binary sensor."""
return DeviceInfo(
identifiers={(DOMAIN, self._service_location.device_serial_number)},
self._attr_device_info = DeviceInfo(
identifiers={(DOMAIN, service_location.device_serial_number)},
manufacturer="Smappee",
model=self._service_location.device_model,
name=self._service_location.service_location_name,
sw_version=self._service_location.firmware_version,
model=service_location.device_model,
name=service_location.service_location_name,
sw_version=service_location.firmware_version,
)
async def async_update(self) -> None:
"""Get the latest data from Smappee and update the state."""
await self._smappee_base.async_update()
self._state = self._service_location.is_present
self._attr_is_on = self._service_location.is_present
class SmappeeAppliance(BinarySensorEntity):
@ -113,70 +109,28 @@ class SmappeeAppliance(BinarySensorEntity):
self._smappee_base = smappee_base
self._service_location = service_location
self._appliance_id = appliance_id
self._appliance_name = appliance_name
self._appliance_type = appliance_type
self._state = False
@property
def name(self):
"""Return the name of the sensor."""
return (
f"{self._service_location.service_location_name} - "
self._attr_name = (
f"{service_location.service_location_name} - "
f"{BINARY_SENSOR_PREFIX} - "
f"{self._appliance_name if self._appliance_name != '' else self._appliance_type}"
f"{appliance_name if appliance_name != '' else appliance_type}"
)
@property
def is_on(self):
"""Return if the binary sensor is turned on."""
return self._state
@property
def icon(self):
"""Icon to use in the frontend."""
icon_mapping = {
"Car Charger": "mdi:car",
"Coffeemaker": "mdi:coffee",
"Clothes Dryer": "mdi:tumble-dryer",
"Clothes Iron": "mdi:hanger",
"Dishwasher": "mdi:dishwasher",
"Lights": "mdi:lightbulb",
"Fan": "mdi:fan",
"Freezer": "mdi:fridge",
"Microwave": "mdi:microwave",
"Oven": "mdi:stove",
"Refrigerator": "mdi:fridge",
"Stove": "mdi:stove",
"Washing Machine": "mdi:washing-machine",
"Water Pump": "mdi:water-pump",
}
return icon_mapping.get(self._appliance_type)
@property
def unique_id(
self,
):
"""Return the unique ID for this binary sensor."""
return (
f"{self._service_location.device_serial_number}-"
f"{self._service_location.service_location_id}-"
f"appliance-{self._appliance_id}"
self._attr_unique_id = (
f"{service_location.device_serial_number}-"
f"{service_location.service_location_id}-"
f"appliance-{appliance_id}"
)
@property
def device_info(self) -> DeviceInfo:
"""Return the device info for this binary sensor."""
return DeviceInfo(
identifiers={(DOMAIN, self._service_location.device_serial_number)},
self._attr_device_info = DeviceInfo(
identifiers={(DOMAIN, service_location.device_serial_number)},
manufacturer="Smappee",
model=self._service_location.device_model,
name=self._service_location.service_location_name,
sw_version=self._service_location.firmware_version,
model=service_location.device_model,
name=service_location.service_location_name,
sw_version=service_location.firmware_version,
)
self._attr_icon = ICON_MAPPING.get(appliance_type)
async def async_update(self) -> None:
"""Get the latest data from Smappee and update the state."""
await self._smappee_base.async_update()
appliance = self._service_location.appliances.get(self._appliance_id)
self._state = bool(appliance.state)
self._attr_is_on = bool(appliance.state)

View File

@ -341,6 +341,13 @@ class SmappeeSensor(SensorEntity):
self.entity_description = description
self._smappee_base = smappee_base
self._service_location = service_location
self._attr_device_info = DeviceInfo(
identifiers={(DOMAIN, service_location.device_serial_number)},
manufacturer="Smappee",
model=service_location.device_model,
name=service_location.service_location_name,
sw_version=service_location.firmware_version,
)
@property
def name(self):
@ -372,17 +379,6 @@ class SmappeeSensor(SensorEntity):
f"{sensor_key}"
)
@property
def device_info(self) -> DeviceInfo:
"""Return the device info for this sensor."""
return DeviceInfo(
identifiers={(DOMAIN, self._service_location.device_serial_number)},
manufacturer="Smappee",
model=self._service_location.device_model,
name=self._service_location.service_location_name,
sw_version=self._service_location.firmware_version,
)
async def async_update(self) -> None:
"""Get the latest data from Smappee and update the state."""
await self._smappee_base.async_update()

View File

@ -74,10 +74,17 @@ class SmappeeActuator(SwitchEntity):
self._actuator_type = actuator_type
self._actuator_serialnumber = actuator_serialnumber
self._actuator_state_option = actuator_state_option
self._state = self._service_location.actuators.get(actuator_id).state
self._connection_state = self._service_location.actuators.get(
self._state = service_location.actuators.get(actuator_id).state
self._connection_state = service_location.actuators.get(
actuator_id
).connection_state
self._attr_device_info = DeviceInfo(
identifiers={(DOMAIN, service_location.device_serial_number)},
manufacturer="Smappee",
model=service_location.device_model,
name=service_location.service_location_name,
sw_version=service_location.firmware_version,
)
@property
def name(self):
@ -153,17 +160,6 @@ class SmappeeActuator(SwitchEntity):
f"{self._actuator_id}"
)
@property
def device_info(self) -> DeviceInfo:
"""Return the device info for this switch."""
return DeviceInfo(
identifiers={(DOMAIN, self._service_location.device_serial_number)},
manufacturer="Smappee",
model=self._service_location.device_model,
name=self._service_location.service_location_name,
sw_version=self._service_location.firmware_version,
)
async def async_update(self) -> None:
"""Get the latest data from Smappee and update the state."""
await self._smappee_base.async_update()