"""Support for the Philips Hue sensor devices."""

from typing import override

from homeassistant.helpers import device_registry as dr, entity
from homeassistant.helpers.device_registry import DeviceInfo

from ..const import CONF_ALLOW_UNREACHABLE, DEFAULT_ALLOW_UNREACHABLE, DOMAIN


class GenericHueDevice(entity.Entity):  # pylint: disable=home-assistant-enforce-class-module
    """Representation of a Hue device."""

    def __init__(self, sensor, name, bridge, primary_sensor=None):
        """Initialize the sensor."""
        self.sensor = sensor
        self._name = name
        self._primary_sensor = primary_sensor
        self.bridge = bridge
        self.allow_unreachable = bridge.config_entry.options.get(
            CONF_ALLOW_UNREACHABLE, DEFAULT_ALLOW_UNREACHABLE
        )

    @property
    def primary_sensor(self):
        """Return the primary sensor entity of the physical device."""
        return self._primary_sensor or self.sensor

    @property
    def device_id(self):
        """Return the ID of the physical device this sensor is part of."""
        return self.unique_id[:23]

    @property
    @override
    def unique_id(self):
        """Return the ID of this Hue sensor."""
        return self.sensor.uniqueid

    @property
    @override
    def name(self):
        """Return a friendly name for the sensor."""
        return self._name

    @property
    def swupdatestate(self):
        """Return detail of available software updates for this device."""
        return self.primary_sensor.raw.get("swupdate", {}).get("state")

    @property
    @override
    def device_info(self) -> DeviceInfo:
        """Return the device info.

        Links individual entities together in the hass device registry.
        """
        return DeviceInfo(
            identifiers={(DOMAIN, self.device_id)},
            manufacturer=self.primary_sensor.manufacturername,
            model=(self.primary_sensor.productname or self.primary_sensor.modelid),
            name=self.primary_sensor.name,
            sw_version=self.primary_sensor.swversion,
            via_device_id=dr.async_get_device_id_by_identifier(
                self.bridge.hass,
                (DOMAIN, self.bridge.api.config.bridgeid),
                config_entry_id=self.bridge.config_entry.entry_id,
            ),
        )
