Use entity attributes in Obihai sensor (#93564)
parent
c25ccb90a4
commit
49ae298c55
|
@ -50,65 +50,37 @@ class ObihaiServiceSensors(SensorEntity):
|
||||||
def __init__(self, pyobihai: PyObihai, serial: str, service_name: str) -> None:
|
def __init__(self, pyobihai: PyObihai, serial: str, service_name: str) -> None:
|
||||||
"""Initialize monitor sensor."""
|
"""Initialize monitor sensor."""
|
||||||
self._service_name = service_name
|
self._service_name = service_name
|
||||||
self._state = None
|
self._attr_name = f"{OBIHAI} {self._service_name}"
|
||||||
self._name = f"{OBIHAI} {self._service_name}"
|
|
||||||
self._pyobihai = pyobihai
|
self._pyobihai = pyobihai
|
||||||
self._unique_id = f"{serial}-{self._service_name}"
|
self._attr_unique_id = f"{serial}-{self._service_name}"
|
||||||
|
|
||||||
@property
|
|
||||||
def name(self):
|
|
||||||
"""Return the name of the sensor."""
|
|
||||||
return self._name
|
|
||||||
|
|
||||||
@property
|
|
||||||
def native_value(self):
|
|
||||||
"""Return the state of the sensor."""
|
|
||||||
return self._state
|
|
||||||
|
|
||||||
@property
|
|
||||||
def available(self):
|
|
||||||
"""Return if sensor is available."""
|
|
||||||
if self._state is not None:
|
|
||||||
return True
|
|
||||||
return False
|
|
||||||
|
|
||||||
@property
|
|
||||||
def unique_id(self):
|
|
||||||
"""Return the unique ID."""
|
|
||||||
return self._unique_id
|
|
||||||
|
|
||||||
@property
|
|
||||||
def device_class(self):
|
|
||||||
"""Return the device class for uptime sensor."""
|
|
||||||
if self._service_name == "Last Reboot":
|
if self._service_name == "Last Reboot":
|
||||||
return SensorDeviceClass.TIMESTAMP
|
self._attr_device_class = SensorDeviceClass.TIMESTAMP
|
||||||
return None
|
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def icon(self):
|
def icon(self) -> str:
|
||||||
"""Return an icon."""
|
"""Return an icon."""
|
||||||
if self._service_name == "Call Direction":
|
if self._service_name == "Call Direction":
|
||||||
if self._state == "No Active Calls":
|
if self._attr_native_value == "No Active Calls":
|
||||||
return "mdi:phone-off"
|
return "mdi:phone-off"
|
||||||
if self._state == "Inbound Call":
|
if self._attr_native_value == "Inbound Call":
|
||||||
return "mdi:phone-incoming"
|
return "mdi:phone-incoming"
|
||||||
return "mdi:phone-outgoing"
|
return "mdi:phone-outgoing"
|
||||||
if "Caller Info" in self._service_name:
|
if "Caller Info" in self._service_name:
|
||||||
return "mdi:phone-log"
|
return "mdi:phone-log"
|
||||||
if "Port" in self._service_name:
|
if "Port" in self._service_name:
|
||||||
if self._state == "Ringing":
|
if self._attr_native_value == "Ringing":
|
||||||
return "mdi:phone-ring"
|
return "mdi:phone-ring"
|
||||||
if self._state == "Off Hook":
|
if self._attr_native_value == "Off Hook":
|
||||||
return "mdi:phone-in-talk"
|
return "mdi:phone-in-talk"
|
||||||
return "mdi:phone-hangup"
|
return "mdi:phone-hangup"
|
||||||
if "Service Status" in self._service_name:
|
if "Service Status" in self._service_name:
|
||||||
if "OBiTALK Service Status" in self._service_name:
|
if "OBiTALK Service Status" in self._service_name:
|
||||||
return "mdi:phone-check"
|
return "mdi:phone-check"
|
||||||
if self._state == "0":
|
if self._attr_native_value == "0":
|
||||||
return "mdi:phone-hangup"
|
return "mdi:phone-hangup"
|
||||||
return "mdi:phone-in-talk"
|
return "mdi:phone-in-talk"
|
||||||
if "Reboot Required" in self._service_name:
|
if "Reboot Required" in self._service_name:
|
||||||
if self._state == "false":
|
if self._attr_native_value == "false":
|
||||||
return "mdi:restart-off"
|
return "mdi:restart-off"
|
||||||
return "mdi:restart-alert"
|
return "mdi:restart-alert"
|
||||||
return "mdi:phone"
|
return "mdi:phone"
|
||||||
|
@ -116,20 +88,25 @@ class ObihaiServiceSensors(SensorEntity):
|
||||||
def update(self) -> None:
|
def update(self) -> None:
|
||||||
"""Update the sensor."""
|
"""Update the sensor."""
|
||||||
if not self._pyobihai.check_account():
|
if not self._pyobihai.check_account():
|
||||||
self._state = None
|
self._attr_native_value = None
|
||||||
|
self._attr_available = False
|
||||||
return
|
return
|
||||||
|
|
||||||
services = self._pyobihai.get_state()
|
services = self._pyobihai.get_state()
|
||||||
|
|
||||||
if self._service_name in services:
|
if self._service_name in services:
|
||||||
self._state = services.get(self._service_name)
|
self._attr_native_value = services.get(self._service_name)
|
||||||
|
|
||||||
services = self._pyobihai.get_line_state()
|
services = self._pyobihai.get_line_state()
|
||||||
|
|
||||||
if services is not None and self._service_name in services:
|
if services is not None and self._service_name in services:
|
||||||
self._state = services.get(self._service_name)
|
self._attr_native_value = services.get(self._service_name)
|
||||||
|
|
||||||
call_direction = self._pyobihai.get_call_direction()
|
call_direction = self._pyobihai.get_call_direction()
|
||||||
|
|
||||||
if self._service_name in call_direction:
|
if self._service_name in call_direction:
|
||||||
self._state = call_direction.get(self._service_name)
|
self._attr_native_value = call_direction.get(self._service_name)
|
||||||
|
|
||||||
|
if self._attr_native_value is None:
|
||||||
|
self._attr_available = False
|
||||||
|
self._attr_available = True
|
||||||
|
|
Loading…
Reference in New Issue