Esempio n. 1
0
async def async_get_conditions(
    hass: HomeAssistant, device_id: str
) -> list[dict[str, str]]:
    """List device conditions for Humidifier devices."""
    registry = await entity_registry.async_get_registry(hass)
    conditions = await toggle_entity.async_get_conditions(hass, device_id, DOMAIN)

    # Get all the integrations entities for this device
    for entry in entity_registry.async_entries_for_device(registry, device_id):
        if entry.domain != DOMAIN:
            continue

        supported_features = get_supported_features(hass, entry.entity_id)

        if supported_features & const.SUPPORT_MODES:
            conditions.append(
                {
                    CONF_CONDITION: "device",
                    CONF_DEVICE_ID: device_id,
                    CONF_DOMAIN: DOMAIN,
                    CONF_ENTITY_ID: entry.entity_id,
                    CONF_TYPE: "is_mode",
                }
            )

    return conditions
Esempio n. 2
0
    async def async_sensors_discovered(sensors, mac):
        """Handle discovery of (additional) sensors."""
        platform = sensor.DOMAIN

        device_registry = await hass.helpers.device_registry.async_get_registry(
        )
        entity_registry = await hass.helpers.entity_registry.async_get_registry(
        )
        device = device_registry.async_get_device(
            set(), {(dev_reg.CONNECTION_NETWORK_MAC, mac)})

        if device is None:
            _LOGGER.warning("Got sensors for unknown device mac: %s", mac)
            return

        orphaned_entities = {
            entry.unique_id
            for entry in async_entries_for_device(
                entity_registry, device.id, include_disabled_entities=True)
            if entry.domain == sensor.DOMAIN and entry.platform == DOMAIN
        }
        for (tasmota_sensor_config, discovery_hash) in sensors:
            if tasmota_sensor_config:
                orphaned_entities.discard(tasmota_sensor_config.unique_id)
            await _discover_entity(tasmota_sensor_config, discovery_hash,
                                   platform)
        for unique_id in orphaned_entities:
            entity_id = entity_registry.async_get_entity_id(
                platform, DOMAIN, unique_id)
            if entity_id:
                _LOGGER.debug("Removing entity: %s %s", platform, entity_id)
                entity_registry.async_remove(entity_id)
async def test_rm_pro_sensor_update(hass):
    """Test a successful RM pro sensor update."""
    device = get_device("Office")
    mock_api = device.get_mock_api()
    mock_api.check_sensors.return_value = {"temperature": 25.7}

    device_registry = mock_device_registry(hass)
    entity_registry = mock_registry(hass)

    mock_api, mock_entry = await device.setup_entry(hass, mock_api=mock_api)

    device_entry = device_registry.async_get_device({(DOMAIN,
                                                      mock_entry.unique_id)})
    entries = async_entries_for_device(entity_registry, device_entry.id)
    sensors = {entry for entry in entries if entry.domain == SENSOR_DOMAIN}
    assert len(sensors) == 1

    mock_api.check_sensors.return_value = {"temperature": 25.8}
    await hass.helpers.entity_component.async_update_entity(
        next(iter(sensors)).entity_id)
    assert mock_api.check_sensors.call_count == 2

    sensors_and_states = {(sensor.original_name,
                           hass.states.get(sensor.entity_id).state)
                          for sensor in sensors}
    assert sensors_and_states == {(f"{device.name} Temperature", "25.8")}
Esempio n. 4
0
async def async_get_actions(hass: HomeAssistant, device_id: str) -> list[dict]:
    """List device actions for Fan devices."""
    registry = await entity_registry.async_get_registry(hass)
    actions = []

    # Get all the integrations entities for this device
    for entry in entity_registry.async_entries_for_device(registry, device_id):
        if entry.domain != DOMAIN:
            continue

        actions.append(
            {
                CONF_DEVICE_ID: device_id,
                CONF_DOMAIN: DOMAIN,
                CONF_ENTITY_ID: entry.entity_id,
                CONF_TYPE: "turn_on",
            }
        )
        actions.append(
            {
                CONF_DEVICE_ID: device_id,
                CONF_DOMAIN: DOMAIN,
                CONF_ENTITY_ID: entry.entity_id,
                CONF_TYPE: "turn_off",
            }
        )

    return actions
Esempio n. 5
0
async def async_get_conditions(hass: HomeAssistant,
                               device_id: str) -> list[dict[str, str]]:
    """List device conditions."""
    conditions: list[dict[str, str]] = []
    entity_registry = er.async_get(hass)
    entries = [
        entry
        for entry in er.async_entries_for_device(entity_registry, device_id)
        if entry.domain == DOMAIN
    ]

    for entry in entries:
        device_class = get_device_class(hass,
                                        entry.entity_id) or DEVICE_CLASS_NONE
        state_class = get_capability(hass, entry.entity_id, ATTR_STATE_CLASS)
        unit_of_measurement = get_unit_of_measurement(hass, entry.entity_id)

        if not unit_of_measurement and not state_class:
            continue

        templates = ENTITY_CONDITIONS.get(device_class,
                                          ENTITY_CONDITIONS[DEVICE_CLASS_NONE])

        conditions.extend({
            **template,
            "condition": "device",
            "device_id": device_id,
            "entity_id": entry.entity_id,
            "domain": DOMAIN,
        } for template in templates)

    return conditions
Esempio n. 6
0
async def _async_get_automations(
    hass: HomeAssistant,
    device_id: str,
    automation_templates: list[dict[str, str]],
    domain: str,
) -> list[dict[str, str]]:
    """List device automations."""
    automations: list[dict[str, str]] = []
    entity_registry = er.async_get(hass)

    entries = [
        entry
        for entry in er.async_entries_for_device(entity_registry, device_id)
        if entry.domain == domain
    ]

    for entry in entries:
        automations.extend({
            **template,
            "device_id": device_id,
            "entity_id": entry.entity_id,
            "domain": domain,
        } for template in automation_templates)

    return automations
Esempio n. 7
0
async def async_get_conditions(hass: HomeAssistant,
                               device_id: str) -> List[Dict[str, str]]:
    """List device conditions for Climate devices."""
    registry = await entity_registry.async_get_registry(hass)
    conditions = []

    # Get all the integrations entities for this device
    for entry in entity_registry.async_entries_for_device(registry, device_id):
        if entry.domain != DOMAIN:
            continue

        state = hass.states.get(entry.entity_id)

        conditions.append({
            CONF_CONDITION: "device",
            CONF_DEVICE_ID: device_id,
            CONF_DOMAIN: DOMAIN,
            CONF_ENTITY_ID: entry.entity_id,
            CONF_TYPE: "is_hvac_mode",
        })

        if state and state.attributes[
                "supported_features"] & const.SUPPORT_PRESET_MODE:
            conditions.append({
                CONF_CONDITION: "device",
                CONF_DEVICE_ID: device_id,
                CONF_DOMAIN: DOMAIN,
                CONF_ENTITY_ID: entry.entity_id,
                CONF_TYPE: "is_preset_mode",
            })

    return conditions
Esempio n. 8
0
async def async_get_config_entry_diagnostics(
    hass: HomeAssistant,
    config_entry: ConfigEntry,
) -> dict[str, Any]:
    """Return diagnostics for a config entry."""
    coordinator = hass.data[DOMAIN][config_entry.entry_id]
    device_registry = dr.async_get(hass)
    entity_registry = er.async_get(hass)

    devices = []

    registry_devices = dr.async_entries_for_config_entry(
        device_registry, config_entry.entry_id)

    for device in registry_devices:
        entities = []

        registry_entities = er.async_entries_for_device(
            entity_registry,
            device_id=device.id,
            include_disabled_entities=True,
        )

        for entity in registry_entities:
            state_dict = None
            if state := hass.states.get(entity.entity_id):
                state_dict = dict(state.as_dict())
                state_dict.pop("context", None)

            entities.append({"entry": asdict(entity), "state": state_dict})

        devices.append({"device": asdict(device), "entities": entities})
async def async_get_actions(hass: HomeAssistant,
                            device_id: str) -> list[dict[str, str]]:
    """List device actions."""
    actions = await toggle_entity.async_get_actions(hass, device_id, DOMAIN)

    entity_registry = er.async_get(hass)

    for entry in er.async_entries_for_device(entity_registry, device_id):
        if entry.domain != DOMAIN:
            continue

        supported_color_modes = get_supported_color_modes(
            hass, entry.entity_id)
        supported_features = get_supported_features(hass, entry.entity_id)

        base_action = {
            CONF_DEVICE_ID: device_id,
            CONF_DOMAIN: DOMAIN,
            CONF_ENTITY_ID: entry.entity_id,
        }

        if brightness_supported(supported_color_modes):
            actions.extend((
                {
                    **base_action, CONF_TYPE: TYPE_BRIGHTNESS_INCREASE
                },
                {
                    **base_action, CONF_TYPE: TYPE_BRIGHTNESS_DECREASE
                },
            ))

        if supported_features & LightEntityFeature.FLASH:
            actions.append({**base_action, CONF_TYPE: TYPE_FLASH})

    return actions
Esempio n. 10
0
async def _async_get_device_automations(hass, automation_type, device_id):
    """List device automations."""
    device_registry, entity_registry = await asyncio.gather(
        hass.helpers.device_registry.async_get_registry(),
        hass.helpers.entity_registry.async_get_registry(),
    )

    domains = set()
    automations: list[MutableMapping[str, Any]] = []
    device = device_registry.async_get(device_id)

    if device is None:
        raise DeviceNotFound

    for entry_id in device.config_entries:
        config_entry = hass.config_entries.async_get_entry(entry_id)
        domains.add(config_entry.domain)

    entity_entries = async_entries_for_device(entity_registry, device_id)
    for entity_entry in entity_entries:
        domains.add(entity_entry.domain)

    device_automations = await asyncio.gather(
        *(
            _async_get_device_automations_from_domain(
                hass, domain, automation_type, device_id
            )
            for domain in domains
        )
    )
    for device_automation in device_automations:
        if device_automation is not None:
            automations.extend(device_automation)

    return automations
Esempio n. 11
0
async def test_device_info(hass: HomeAssistant) -> None:
    """Verify device information includes expected details."""
    entry = await setup_mock_motioneye_config_entry(hass)

    device_identifier = get_motioneye_device_identifier(
        entry.entry_id, TEST_CAMERA_ID)
    device_registry = dr.async_get(hass)

    device = device_registry.async_get_device({device_identifier})
    assert device

    # Test device details here (not tested for other platforms as the are set
    # centrally).
    assert device.config_entries == {TEST_CONFIG_ENTRY_ID}
    assert device.identifiers == {device_identifier}
    assert device.manufacturer == MOTIONEYE_MANUFACTURER
    assert device.model == MOTIONEYE_MANUFACTURER
    assert device.name == TEST_CAMERA_NAME

    entity_registry = await er.async_get_registry(hass)
    entities_from_device = [
        entry.entity_id
        for entry in er.async_entries_for_device(entity_registry, device.id)
    ]
    assert TEST_CAMERA_ENTITY_ID in entities_from_device
async def async_get_actions(hass: HomeAssistant,
                            device_id: str) -> list[dict[str, str]]:
    """List device actions for Alarm control panel devices."""
    registry = await entity_registry.async_get_registry(hass)
    actions = []

    # Get all the integrations entities for this device
    for entry in entity_registry.async_entries_for_device(registry, device_id):
        if entry.domain != DOMAIN:
            continue

        supported_features = get_supported_features(hass, entry.entity_id)

        base_action: dict = {
            CONF_DEVICE_ID: device_id,
            CONF_DOMAIN: DOMAIN,
            CONF_ENTITY_ID: entry.entity_id,
        }

        # Add actions for each entity that belongs to this integration
        if supported_features & SUPPORT_ALARM_ARM_AWAY:
            actions.append({**base_action, CONF_TYPE: "arm_away"})
        if supported_features & SUPPORT_ALARM_ARM_HOME:
            actions.append({**base_action, CONF_TYPE: "arm_home"})
        if supported_features & SUPPORT_ALARM_ARM_NIGHT:
            actions.append({**base_action, CONF_TYPE: "arm_night"})
        if supported_features & SUPPORT_ALARM_ARM_VACATION:
            actions.append({**base_action, CONF_TYPE: "arm_vacation"})
        actions.append({**base_action, CONF_TYPE: "disarm"})
        if supported_features & SUPPORT_ALARM_TRIGGER:
            actions.append({**base_action, CONF_TYPE: "trigger"})

    return actions
Esempio n. 13
0
async def async_get_triggers(
    hass: HomeAssistant, device_id: str
) -> list[dict[str, Any]]:
    """List device triggers for Lock devices."""
    registry = await entity_registry.async_get_registry(hass)
    triggers = []

    # Get all the integrations entities for this device
    for entry in entity_registry.async_entries_for_device(registry, device_id):
        if entry.domain != DOMAIN:
            continue

        # Add triggers for each entity that belongs to this integration
        triggers += [
            {
                CONF_PLATFORM: "device",
                CONF_DEVICE_ID: device_id,
                CONF_DOMAIN: DOMAIN,
                CONF_ENTITY_ID: entry.entity_id,
                CONF_TYPE: trigger,
            }
            for trigger in TRIGGER_TYPES
        ]

    return triggers
Esempio n. 14
0
async def test_device_setup_registry(hass):
    """Test we register the device and the entries correctly."""
    device = get_device("Office")
    mock_api = device.get_mock_api()
    mock_entry = device.get_mock_entry()
    mock_entry.add_to_hass(hass)

    device_registry = mock_device_registry(hass)
    entity_registry = mock_registry(hass)

    with patch("broadlink.gendevice", return_value=mock_api):
        await hass.config_entries.async_setup(mock_entry.entry_id)
        await hass.async_block_till_done()

    assert len(device_registry.devices) == 1

    device_entry = device_registry.async_get_device(
        {(DOMAIN, mock_entry.unique_id)}, set())
    assert device_entry.identifiers == {(DOMAIN, device.mac)}
    assert device_entry.name == device.name
    assert device_entry.model == device.model
    assert device_entry.manufacturer == device.manufacturer
    assert device_entry.sw_version == device.fwversion

    for entry in async_entries_for_device(entity_registry, device_entry.id):
        assert entry.original_name.startswith(device.name)
Esempio n. 15
0
async def async_get_triggers(hass, device_id):
    """List device triggers."""
    triggers = []
    entity_registry = await hass.helpers.entity_registry.async_get_registry()

    entries = [
        entry for entry in async_entries_for_device(entity_registry, device_id)
        if entry.domain == DOMAIN
    ]

    for entry in entries:
        device_class = DEVICE_CLASS_NONE
        state = hass.states.get(entry.entity_id)
        unit_of_measurement = (state.attributes.get(ATTR_UNIT_OF_MEASUREMENT)
                               if state else None)

        if not state or not unit_of_measurement:
            continue

        if ATTR_DEVICE_CLASS in state.attributes:
            device_class = state.attributes[ATTR_DEVICE_CLASS]

        templates = ENTITY_TRIGGERS.get(device_class,
                                        ENTITY_TRIGGERS[DEVICE_CLASS_NONE])

        triggers.extend({
            **automation,
            "platform": "device",
            "device_id": device_id,
            "entity_id": entry.entity_id,
            "domain": DOMAIN,
        } for automation in templates)

    return triggers
Esempio n. 16
0
async def async_get_actions(hass: HomeAssistant, device_id: str) -> list[dict]:
    """List device actions for Humidifier devices."""
    registry = await entity_registry.async_get_registry(hass)
    actions = await toggle_entity.async_get_actions(hass, device_id, DOMAIN)

    # Get all the integrations entities for this device
    for entry in entity_registry.async_entries_for_device(registry, device_id):
        if entry.domain != DOMAIN:
            continue

        state = hass.states.get(entry.entity_id)

        actions.append({
            CONF_DEVICE_ID: device_id,
            CONF_DOMAIN: DOMAIN,
            CONF_ENTITY_ID: entry.entity_id,
            CONF_TYPE: "set_humidity",
        })

        # We need a state or else we can't populate the available modes.
        if state is None:
            continue

        if state.attributes[ATTR_SUPPORTED_FEATURES] & const.SUPPORT_MODES:
            actions.append({
                CONF_DEVICE_ID: device_id,
                CONF_DOMAIN: DOMAIN,
                CONF_ENTITY_ID: entry.entity_id,
                CONF_TYPE: "set_mode",
            })

    return actions
async def async_get_triggers(
    hass: HomeAssistant, device_id: str
) -> list[dict[str, Any]]:
    """List device triggers for Device Tracker devices."""
    registry = await entity_registry.async_get_registry(hass)
    triggers = []

    # Get all the integrations entities for this device
    for entry in entity_registry.async_entries_for_device(registry, device_id):
        if entry.domain != DOMAIN:
            continue

        triggers.append(
            {
                CONF_PLATFORM: "device",
                CONF_DEVICE_ID: device_id,
                CONF_DOMAIN: DOMAIN,
                CONF_ENTITY_ID: entry.entity_id,
                CONF_TYPE: "enters",
            }
        )
        triggers.append(
            {
                CONF_PLATFORM: "device",
                CONF_DEVICE_ID: device_id,
                CONF_DOMAIN: DOMAIN,
                CONF_ENTITY_ID: entry.entity_id,
                CONF_TYPE: "leaves",
            }
        )

    return triggers
Esempio n. 18
0
async def async_get_triggers(hass, device_id):
    """List device triggers."""
    triggers = []
    entity_registry = await hass.helpers.entity_registry.async_get_registry()

    entries = [
        entry for entry in async_entries_for_device(entity_registry, device_id)
        if entry.domain == DOMAIN
    ]

    for entry in entries:
        device_class = get_device_class(hass,
                                        entry.entity_id) or DEVICE_CLASS_NONE
        unit_of_measurement = get_unit_of_measurement(hass, entry.entity_id)

        if not unit_of_measurement:
            continue

        templates = ENTITY_TRIGGERS.get(device_class,
                                        ENTITY_TRIGGERS[DEVICE_CLASS_NONE])

        triggers.extend({
            **automation,
            "platform": "device",
            "device_id": device_id,
            "entity_id": entry.entity_id,
            "domain": DOMAIN,
        } for automation in templates)

    return triggers
Esempio n. 19
0
 def determine_entity_domains(hass: HomeAssistantType,
                              group: zha_typing.ZhaGroupType) -> List[str]:
     """Determine the entity domains for this group."""
     entity_domains: List[str] = []
     zha_gateway = hass.data[zha_const.DATA_ZHA][zha_const.DATA_ZHA_GATEWAY]
     all_domain_occurrences = []
     for member in group.members:
         if member.device.is_coordinator:
             continue
         entities = async_entries_for_device(
             zha_gateway.ha_entity_registry,
             member.device.device_id,
             include_disabled_entities=True,
         )
         all_domain_occurrences.extend([
             entity.domain for entity in entities
             if entity.domain in zha_regs.GROUP_ENTITY_DOMAINS
         ])
     if not all_domain_occurrences:
         return entity_domains
     # get all domains we care about if there are more than 2 entities of this domain
     counts = Counter(all_domain_occurrences)
     entity_domains = [
         domain[0] for domain in counts.items() if domain[1] >= 2
     ]
     _LOGGER.debug(
         "The entity domains are: %s for group: %s:0x%04x",
         entity_domains,
         group.name,
         group.group_id,
     )
     return entity_domains
Esempio n. 20
0
    def _cleanup_group_entity_registry_entries(
            self, zigpy_group: zigpy.group.Group) -> None:
        """Remove entity registry entries for group entities when the groups are removed from HA."""
        # first we collect the potential unique ids for entities that could be created from this group
        possible_entity_unique_ids = [
            f"{domain}_zha_group_0x{zigpy_group.group_id:04x}"
            for domain in GROUP_ENTITY_DOMAINS
        ]

        # then we get all group entity entries tied to the coordinator
        all_group_entity_entries = async_entries_for_device(
            self.ha_entity_registry,
            self.coordinator_zha_device.device_id,
            include_disabled_entities=True,
        )

        # then we get the entity entries for this specific group by getting the entries that match
        entries_to_remove = [
            entry for entry in all_group_entity_entries
            if entry.unique_id in possible_entity_unique_ids
        ]

        # then we remove the entries from the entity registry
        for entry in entries_to_remove:
            _LOGGER.debug("cleaning up entity registry entry for entity: %s",
                          entry.entity_id)
            self.ha_entity_registry.async_remove(entry.entity_id)
Esempio n. 21
0
async def test_device_info(hass: HomeAssistant) -> None:
    """Verify device information includes expected details."""
    client = create_mock_client()

    register_test_entity(
        hass,
        CAMERA_DOMAIN,
        TYPE_HYPERION_CAMERA,
        TEST_CAMERA_ENTITY_ID,
    )
    await setup_test_config_entry(hass, hyperion_client=client)

    device_id = get_hyperion_device_id(TEST_SYSINFO_ID, TEST_INSTANCE)
    device_registry = dr.async_get(hass)

    device = device_registry.async_get_device({(DOMAIN, device_id)})
    assert device
    assert device.config_entries == {TEST_CONFIG_ENTRY_ID}
    assert device.identifiers == {(DOMAIN, device_id)}
    assert device.manufacturer == HYPERION_MANUFACTURER_NAME
    assert device.model == HYPERION_MODEL_NAME
    assert device.name == TEST_INSTANCE_1["friendly_name"]

    entity_registry = await er.async_get_registry(hass)
    entities_from_device = [
        entry.entity_id
        for entry in er.async_entries_for_device(entity_registry, device.id)
    ]
    assert TEST_CAMERA_ENTITY_ID in entities_from_device
Esempio n. 22
0
async def test_remote_send_command(hass):
    """Test sending a command with all remotes."""
    for device in map(get_device, REMOTE_DEVICES):
        device_registry = mock_device_registry(hass)
        entity_registry = mock_registry(hass)
        mock_setup = await device.setup_entry(hass)

        device_entry = device_registry.async_get_device(
            {(DOMAIN, mock_setup.entry.unique_id)}
        )
        entries = async_entries_for_device(entity_registry, device_entry.id)
        remotes = [entry for entry in entries if entry.domain == Platform.REMOTE]
        assert len(remotes) == 1

        remote = remotes[0]
        await hass.services.async_call(
            REMOTE_DOMAIN,
            SERVICE_SEND_COMMAND,
            {"entity_id": remote.entity_id, "command": "b64:" + IR_PACKET},
            blocking=True,
        )

        assert mock_setup.api.send_data.call_count == 1
        assert mock_setup.api.send_data.call_args == call(b64decode(IR_PACKET))
        assert mock_setup.api.auth.call_count == 1
async def async_get_conditions(hass: HomeAssistant,
                               device_id: str) -> List[Dict[str, str]]:
    """List device conditions for NEW_NAME devices."""
    registry = await entity_registry.async_get_registry(hass)
    conditions = []

    # Get all the integrations entities for this device
    for entry in entity_registry.async_entries_for_device(registry, device_id):
        if entry.domain != DOMAIN:
            continue

        # Add conditions for each entity that belongs to this integration
        # TODO add your own conditions.
        conditions.append({
            CONF_CONDITION: "device",
            CONF_DEVICE_ID: device_id,
            CONF_DOMAIN: DOMAIN,
            CONF_ENTITY_ID: entry.entity_id,
            CONF_TYPE: "is_on",
        })
        conditions.append({
            CONF_CONDITION: "device",
            CONF_DEVICE_ID: device_id,
            CONF_DOMAIN: DOMAIN,
            CONF_ENTITY_ID: entry.entity_id,
            CONF_TYPE: "is_off",
        })

    return conditions
Esempio n. 24
0
async def async_get_conditions(hass: HomeAssistant,
                               device_id: str) -> List[Dict[str, str]]:
    """List device conditions."""
    conditions: List[Dict[str, str]] = []
    entity_registry = await async_get_registry(hass)
    entries = [
        entry for entry in async_entries_for_device(entity_registry, device_id)
        if entry.domain == DOMAIN
    ]

    for entry in entries:
        device_class = DEVICE_CLASS_NONE
        state = hass.states.get(entry.entity_id)
        unit_of_measurement = (state.attributes.get(ATTR_UNIT_OF_MEASUREMENT)
                               if state else None)

        if not state or not unit_of_measurement:
            continue

        if ATTR_DEVICE_CLASS in state.attributes:
            device_class = state.attributes[ATTR_DEVICE_CLASS]

        templates = ENTITY_CONDITIONS.get(device_class,
                                          ENTITY_CONDITIONS[DEVICE_CLASS_NONE])

        conditions.extend({
            **template,
            "condition": "device",
            "device_id": device_id,
            "entity_id": entry.entity_id,
            "domain": DOMAIN,
        } for template in templates)

    return conditions
Esempio n. 25
0
async def async_get_triggers(hass, device_id):
    """List device triggers."""
    triggers = []
    entity_registry = await hass.helpers.entity_registry.async_get_registry()

    entries = [
        entry
        for entry in async_entries_for_device(entity_registry, device_id)
        if entry.domain == DOMAIN
    ]

    for entry in entries:
        device_class = DEVICE_CLASS_NONE
        state = hass.states.get(entry.entity_id)
        if state:
            device_class = state.attributes.get(ATTR_DEVICE_CLASS)

        templates = ENTITY_TRIGGERS.get(
            device_class, ENTITY_TRIGGERS[DEVICE_CLASS_NONE]
        )

        triggers.extend(
            {
                **automation,
                "platform": "device",
                "device_id": device_id,
                "entity_id": entry.entity_id,
                "domain": DOMAIN,
            }
            for automation in templates
        )

    return triggers
Esempio n. 26
0
async def async_get_device_automation_triggers(hass, device_id):
    """List device triggers."""
    device_registry, entity_registry = await asyncio.gather(
        hass.helpers.device_registry.async_get_registry(),
        hass.helpers.entity_registry.async_get_registry())

    domains = set()
    triggers = []
    device = device_registry.async_get(device_id)
    for entry_id in device.config_entries:
        config_entry = hass.config_entries.async_get_entry(entry_id)
        domains.add(config_entry.domain)

    entities = async_entries_for_device(entity_registry, device_id)
    for entity in entities:
        domains.add(split_entity_id(entity.entity_id)[0])

    device_triggers = await asyncio.gather(*[
        _async_get_device_automation_triggers(hass, domain, device_id)
        for domain in domains
    ])
    for device_trigger in device_triggers:
        if device_trigger is not None:
            triggers.extend(device_trigger)

    return triggers
Esempio n. 27
0
async def async_get_conditions(hass: HomeAssistant,
                               device_id: str) -> list[dict[str, str]]:
    """List device conditions."""
    conditions: list[dict[str, str]] = []
    entity_registry = await async_get_registry(hass)
    entries = [
        entry for entry in async_entries_for_device(entity_registry, device_id)
        if entry.domain == DOMAIN
    ]

    for entry in entries:
        device_class = get_device_class(hass,
                                        entry.entity_id) or DEVICE_CLASS_NONE

        templates = ENTITY_CONDITIONS.get(device_class,
                                          ENTITY_CONDITIONS[DEVICE_CLASS_NONE])

        conditions.extend({
            **template,
            "condition": "device",
            "device_id": device_id,
            "entity_id": entry.entity_id,
            "domain": DOMAIN,
        } for template in templates)

    return conditions
Esempio n. 28
0
async def async_get_conditions(hass: HomeAssistant,
                               device_id: str) -> list[dict[str, str]]:
    """List device conditions for Climate devices."""
    registry = entity_registry.async_get(hass)
    conditions = []

    # Get all the integrations entities for this device
    for entry in entity_registry.async_entries_for_device(registry, device_id):
        if entry.domain != DOMAIN:
            continue

        supported_features = get_supported_features(hass, entry.entity_id)

        base_condition = {
            CONF_CONDITION: "device",
            CONF_DEVICE_ID: device_id,
            CONF_DOMAIN: DOMAIN,
            CONF_ENTITY_ID: entry.entity_id,
        }

        conditions.append({**base_condition, CONF_TYPE: "is_hvac_mode"})

        if supported_features & const.SUPPORT_PRESET_MODE:
            conditions.append({**base_condition, CONF_TYPE: "is_preset_mode"})

    return conditions
Esempio n. 29
0
async def test_rm4_pro_hts2_sensor_setup(hass):
    """Test a successful RM4 pro sensor setup with HTS2 cable."""
    device = get_device("Garage")
    mock_api = device.get_mock_api()
    mock_api.check_sensors.return_value = {
        "temperature": 22.5,
        "humidity": 43.7
    }

    device_registry = mock_device_registry(hass)
    entity_registry = mock_registry(hass)

    mock_api, mock_entry = await device.setup_entry(hass, mock_api=mock_api)

    assert mock_api.check_sensors.call_count == 1
    device_entry = device_registry.async_get_device({(DOMAIN,
                                                      mock_entry.unique_id)})
    entries = async_entries_for_device(entity_registry, device_entry.id)
    sensors = {entry for entry in entries if entry.domain == SENSOR_DOMAIN}
    assert len(sensors) == 2

    sensors_and_states = {(sensor.original_name,
                           hass.states.get(sensor.entity_id).state)
                          for sensor in sensors}
    assert sensors_and_states == {
        (f"{device.name} Temperature", "22.5"),
        (f"{device.name} Humidity", "43.7"),
    }
Esempio n. 30
0
async def async_get_triggers(hass: HomeAssistant,
                             device_id: str) -> list[dict]:
    """List device triggers for Kodi devices."""
    registry = await entity_registry.async_get_registry(hass)
    triggers = []

    # Get all the integrations entities for this device
    for entry in entity_registry.async_entries_for_device(registry, device_id):
        if entry.domain == "media_player":
            triggers.append({
                CONF_PLATFORM: "device",
                CONF_DEVICE_ID: device_id,
                CONF_DOMAIN: DOMAIN,
                CONF_ENTITY_ID: entry.entity_id,
                CONF_TYPE: "turn_on",
            })
            triggers.append({
                CONF_PLATFORM: "device",
                CONF_DEVICE_ID: device_id,
                CONF_DOMAIN: DOMAIN,
                CONF_ENTITY_ID: entry.entity_id,
                CONF_TYPE: "turn_off",
            })

    return triggers