"""Support for Ubiquiti mFi sensors."""

import logging
from typing import override

from mficlient.client import FailedToLogin, MFiClient, Port as MFiPort
import requests
import voluptuous as vol

from homeassistant.components.sensor import (
    PLATFORM_SCHEMA as SENSOR_PLATFORM_SCHEMA,
    SensorDeviceClass,
    SensorEntity,
    StateType,
)
from homeassistant.const import (
    CONF_HOST,
    CONF_PASSWORD,
    CONF_PORT,
    CONF_SSL,
    CONF_USERNAME,
    CONF_VERIFY_SSL,
    STATE_OFF,
    STATE_ON,
    UnitOfTemperature,
)
from homeassistant.core import HomeAssistant
from homeassistant.helpers import config_validation as cv
from homeassistant.helpers.entity_platform import AddEntitiesCallback
from homeassistant.helpers.typing import ConfigType, DiscoveryInfoType

_LOGGER = logging.getLogger(__name__)

DEFAULT_SSL = True
DEFAULT_VERIFY_SSL = True

DIGITS = {"volts": 1, "amps": 1, "active_power": 0, "temperature": 1}

SENSOR_MODELS = [
    "Ubiquiti mFi-THS",
    "Ubiquiti mFi-CS",
    "Ubiquiti mFi-DS",
    "Outlet",
    "Input Analog",
    "Input Digital",
]

PLATFORM_SCHEMA = SENSOR_PLATFORM_SCHEMA.extend(
    {
        vol.Required(CONF_HOST): cv.string,
        vol.Required(CONF_USERNAME): cv.string,
        vol.Required(CONF_PASSWORD): cv.string,
        vol.Optional(CONF_PORT): cv.port,
        vol.Optional(CONF_SSL, default=DEFAULT_SSL): cv.boolean,
        vol.Optional(CONF_VERIFY_SSL, default=DEFAULT_VERIFY_SSL): cv.boolean,
    }
)


def setup_platform(
    hass: HomeAssistant,
    config: ConfigType,
    add_entities: AddEntitiesCallback,
    discovery_info: DiscoveryInfoType | None = None,
) -> None:
    """Set up mFi sensors."""
    host: str = config[CONF_HOST]
    username: str = config[CONF_USERNAME]
    password: str = config[CONF_PASSWORD]
    use_tls: bool = config[CONF_SSL]
    verify_tls: bool = config[CONF_VERIFY_SSL]
    default_port = 6443 if use_tls else 6080
    network_port: int = config.get(CONF_PORT, default_port)

    try:
        client = MFiClient(
            host,
            username,
            password,
            port=network_port,
            use_tls=use_tls,
            verify=verify_tls,
        )
    except (FailedToLogin, requests.exceptions.ConnectionError) as ex:
        _LOGGER.error("Unable to connect to mFi: %s", str(ex))
        return

    add_entities(
        MfiSensor(port)
        for device in client.get_devices()
        for port in device.ports.values()
        if port.model in SENSOR_MODELS
    )


class MfiSensor(SensorEntity):
    """Representation of a mFi sensor."""

    def __init__(self, port: MFiPort) -> None:
        """Initialize the sensor."""
        self._port = port

    @property
    @override
    def name(self) -> str:
        """Return the name of the sensor."""
        return self._port.label

    @property
    @override
    def native_value(self) -> StateType:
        """Return the state of the sensor."""
        try:
            tag = self._port.tag
        except ValueError:
            tag = None
        if tag is None:
            return STATE_OFF
        if self._port.model == "Input Digital":
            return STATE_ON if self._port.value > 0 else STATE_OFF
        digits = DIGITS.get(self._port.tag, 0)
        return round(self._port.value, digits)

    @property
    @override
    def device_class(self) -> SensorDeviceClass | None:
        """Return the device class of the sensor."""
        try:
            tag = self._port.tag
        except ValueError:
            return None

        if tag == "temperature":
            return SensorDeviceClass.TEMPERATURE

        return None

    @property
    @override
    def native_unit_of_measurement(self) -> str | None:
        """Return the unit of measurement of this entity, if any."""
        try:
            tag = self._port.tag
        except ValueError:
            return None

        if tag == "temperature":
            return UnitOfTemperature.CELSIUS
        if tag == "active_pwr":
            return "Watts"
        if self._port.model == "Input Digital":
            return None
        return tag

    def update(self) -> None:
        """Get the latest data."""
        self._port.refresh()
