Use entity class attributes for agent_dvr (#52501)

* Use entity class attributes for agent_dvr

* Apply suggestions from code review

Co-authored-by: Milan Meulemans <milan.meulemans@live.be>

* rework

Co-authored-by: Milan Meulemans <milan.meulemans@live.be>
This commit is contained in:
Robert Hillis 2021-07-15 08:57:51 -04:00 committed by GitHub
parent 19b4d2e4d2
commit 00741d4273
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 38 additions and 99 deletions

View File

@ -35,90 +35,60 @@ async def async_setup_entry(
class AgentBaseStation(AlarmControlPanelEntity): class AgentBaseStation(AlarmControlPanelEntity):
"""Representation of an Agent DVR Alarm Control Panel.""" """Representation of an Agent DVR Alarm Control Panel."""
_attr_icon = ICON
_attr_supported_features = (
SUPPORT_ALARM_ARM_HOME | SUPPORT_ALARM_ARM_AWAY | SUPPORT_ALARM_ARM_NIGHT
)
def __init__(self, client): def __init__(self, client):
"""Initialize the alarm control panel.""" """Initialize the alarm control panel."""
self._state = None
self._client = client self._client = client
self._unique_id = f"{client.unique}_CP" self._attr_name = f"{client.name} {CONST_ALARM_CONTROL_PANEL_NAME}"
name = CONST_ALARM_CONTROL_PANEL_NAME self._attr_unique_id = f"{client.unique}_CP"
self._name = name = f"{client.name} {name}" self._attr_device_info = {
"identifiers": {(AGENT_DOMAIN, client.unique)},
@property
def icon(self):
"""Return icon."""
return ICON
@property
def state(self):
"""Return the state of the device."""
return self._state
@property
def supported_features(self) -> int:
"""Return the list of supported features."""
return SUPPORT_ALARM_ARM_HOME | SUPPORT_ALARM_ARM_AWAY | SUPPORT_ALARM_ARM_NIGHT
@property
def device_info(self):
"""Return the device info for adding the entity to the agent object."""
return {
"identifiers": {(AGENT_DOMAIN, self._client.unique)},
"manufacturer": "Agent", "manufacturer": "Agent",
"model": CONST_ALARM_CONTROL_PANEL_NAME, "model": CONST_ALARM_CONTROL_PANEL_NAME,
"sw_version": self._client.version, "sw_version": client.version,
} }
async def async_update(self): async def async_update(self):
"""Update the state of the device.""" """Update the state of the device."""
await self._client.update() await self._client.update()
self._attr_available = self._client.is_available
armed = self._client.is_armed armed = self._client.is_armed
if armed is None: if armed is None:
self._state = None self._attr_state = None
return return
if armed: if armed:
prof = (await self._client.get_active_profile()).lower() prof = (await self._client.get_active_profile()).lower()
self._state = STATE_ALARM_ARMED_AWAY self._attr_state = STATE_ALARM_ARMED_AWAY
if prof == CONF_HOME_MODE_NAME: if prof == CONF_HOME_MODE_NAME:
self._state = STATE_ALARM_ARMED_HOME self._attr_state = STATE_ALARM_ARMED_HOME
elif prof == CONF_NIGHT_MODE_NAME: elif prof == CONF_NIGHT_MODE_NAME:
self._state = STATE_ALARM_ARMED_NIGHT self._attr_state = STATE_ALARM_ARMED_NIGHT
else: else:
self._state = STATE_ALARM_DISARMED self._attr_state = STATE_ALARM_DISARMED
async def async_alarm_disarm(self, code=None): async def async_alarm_disarm(self, code=None):
"""Send disarm command.""" """Send disarm command."""
await self._client.disarm() await self._client.disarm()
self._state = STATE_ALARM_DISARMED self._attr_state = STATE_ALARM_DISARMED
async def async_alarm_arm_away(self, code=None): async def async_alarm_arm_away(self, code=None):
"""Send arm away command. Uses custom mode.""" """Send arm away command. Uses custom mode."""
await self._client.arm() await self._client.arm()
await self._client.set_active_profile(CONF_AWAY_MODE_NAME) await self._client.set_active_profile(CONF_AWAY_MODE_NAME)
self._state = STATE_ALARM_ARMED_AWAY self._attr_state = STATE_ALARM_ARMED_AWAY
async def async_alarm_arm_home(self, code=None): async def async_alarm_arm_home(self, code=None):
"""Send arm home command. Uses custom mode.""" """Send arm home command. Uses custom mode."""
await self._client.arm() await self._client.arm()
await self._client.set_active_profile(CONF_HOME_MODE_NAME) await self._client.set_active_profile(CONF_HOME_MODE_NAME)
self._state = STATE_ALARM_ARMED_HOME self._attr_state = STATE_ALARM_ARMED_HOME
async def async_alarm_arm_night(self, code=None): async def async_alarm_arm_night(self, code=None):
"""Send arm night command. Uses custom mode.""" """Send arm night command. Uses custom mode."""
await self._client.arm() await self._client.arm()
await self._client.set_active_profile(CONF_NIGHT_MODE_NAME) await self._client.set_active_profile(CONF_NIGHT_MODE_NAME)
self._state = STATE_ALARM_ARMED_NIGHT self._attr_state = STATE_ALARM_ARMED_NIGHT
@property
def name(self):
"""Return the name of the base station."""
return self._name
@property
def available(self) -> bool:
"""Device available."""
return self._client.is_available
@property
def unique_id(self) -> str:
"""Return a unique ID."""
return self._unique_id

View File

@ -67,31 +67,27 @@ async def async_setup_entry(
class AgentCamera(MjpegCamera): class AgentCamera(MjpegCamera):
"""Representation of an Agent Device Stream.""" """Representation of an Agent Device Stream."""
_attr_supported_features = SUPPORT_ON_OFF
def __init__(self, device): def __init__(self, device):
"""Initialize as a subclass of MjpegCamera.""" """Initialize as a subclass of MjpegCamera."""
self._servername = device.client.name
self.server_url = device.client._server_url
device_info = { device_info = {
CONF_NAME: device.name, CONF_NAME: device.name,
CONF_MJPEG_URL: f"{self.server_url}{device.mjpeg_image_url}&size={device.mjpegStreamWidth}x{device.mjpegStreamHeight}", CONF_MJPEG_URL: f"{device.client._server_url}{device.mjpeg_image_url}&size={device.mjpegStreamWidth}x{device.mjpegStreamHeight}",
CONF_STILL_IMAGE_URL: f"{self.server_url}{device.still_image_url}&size={device.mjpegStreamWidth}x{device.mjpegStreamHeight}", CONF_STILL_IMAGE_URL: f"{device.client._server_url}{device.still_image_url}&size={device.mjpegStreamWidth}x{device.mjpegStreamHeight}",
} }
self.device = device self.device = device
self._removed = False self._removed = False
self._name = f"{self._servername} {device.name}" self._attr_name = f"{device.client.name} {device.name}"
self._unique_id = f"{device._client.unique}_{device.typeID}_{device.id}" self._attr_unique_id = f"{device._client.unique}_{device.typeID}_{device.id}"
self._attr_should_poll = True
super().__init__(device_info) super().__init__(device_info)
self._attr_device_info = {
@property "identifiers": {(AGENT_DOMAIN, self.unique_id)},
def device_info(self): "name": self.name,
"""Return the device info for adding the entity to the agent object."""
return {
"identifiers": {(AGENT_DOMAIN, self._unique_id)},
"name": self._name,
"manufacturer": "Agent", "manufacturer": "Agent",
"model": "Camera", "model": "Camera",
"sw_version": self.device.client.version, "sw_version": device.client.version,
} }
async def async_update(self): async def async_update(self):
@ -99,18 +95,18 @@ class AgentCamera(MjpegCamera):
try: try:
await self.device.update() await self.device.update()
if self._removed: if self._removed:
_LOGGER.debug("%s reacquired", self._name) _LOGGER.debug("%s reacquired", self.name)
self._removed = False self._removed = False
except AgentError: except AgentError:
# server still available - camera error # server still available - camera error
if self.device.client.is_available and not self._removed: if self.device.client.is_available and not self._removed:
_LOGGER.error("%s lost", self._name) _LOGGER.error("%s lost", self.name)
self._removed = True self._removed = True
self._attr_available = self.device.client.is_available
@property self._attr_icon = "mdi:camcorder-off"
def extra_state_attributes(self): if self.is_on:
"""Return the Agent DVR camera state attributes.""" self._attr_icon = "mdi:camcorder"
return { self._attr_extra_state_attributes = {
ATTR_ATTRIBUTION: ATTRIBUTION, ATTR_ATTRIBUTION: ATTRIBUTION,
"editable": False, "editable": False,
"enabled": self.is_on, "enabled": self.is_on,
@ -121,11 +117,6 @@ class AgentCamera(MjpegCamera):
"alerts_enabled": self.device.alerts_active, "alerts_enabled": self.device.alerts_active,
} }
@property
def should_poll(self) -> bool:
"""Update the state periodically."""
return True
@property @property
def is_recording(self) -> bool: def is_recording(self) -> bool:
"""Return whether the monitor is recording.""" """Return whether the monitor is recording."""
@ -141,43 +132,21 @@ class AgentCamera(MjpegCamera):
"""Return whether the monitor has alerted.""" """Return whether the monitor has alerted."""
return self.device.detected return self.device.detected
@property
def available(self) -> bool:
"""Return True if entity is available."""
return self.device.client.is_available
@property @property
def connected(self) -> bool: def connected(self) -> bool:
"""Return True if entity is connected.""" """Return True if entity is connected."""
return self.device.connected return self.device.connected
@property
def supported_features(self) -> int:
"""Return supported features."""
return SUPPORT_ON_OFF
@property @property
def is_on(self) -> bool: def is_on(self) -> bool:
"""Return true if on.""" """Return true if on."""
return self.device.online return self.device.online
@property
def icon(self):
"""Return the icon to use in the frontend, if any."""
if self.is_on:
return "mdi:camcorder"
return "mdi:camcorder-off"
@property @property
def motion_detection_enabled(self): def motion_detection_enabled(self):
"""Return the camera motion detection status.""" """Return the camera motion detection status."""
return self.device.detector_active return self.device.detector_active
@property
def unique_id(self) -> str:
"""Return a unique identifier for this agent object."""
return self._unique_id
async def async_enable_alerts(self): async def async_enable_alerts(self):
"""Enable alerts.""" """Enable alerts."""
await self.device.alerts_on() await self.device.alerts_on()