def get_unit_of_measurement(hass: HomeAssistant, entity_id: str) -> (str | None):
    'Get unit of measurement class of an entity.\n\n    First try the statemachine, then entity registry.\n    '
    if (state := hass.states.get(entity_id)):
        return state.attributes.get(ATTR_UNIT_OF_MEASUREMENT)
    entity_registry = er.async_get(hass)
    if (not (entry := entity_registry.async_get(entity_id))):
        raise HomeAssistantError(f'Unknown entity {entity_id}')
    return entry.unit_of_measurement
Get unit of measurement class of an entity.

First try the statemachine, then entity registry.
def get_unit_of_measurement(hass: HomeAssistant, entity_id: str) -> (str | None):
    'Get unit of measurement class of an entity.\n\n    First try the statemachine, then entity registry.\n    '
    if (state := hass.states.get(entity_id)):
        return state.attributes.get(ATTR_UNIT_OF_MEASUREMENT)
    entity_registry = er.async_get(hass)
    if (not (entry := entity_registry.async_get(entity_id))):
        raise HomeAssistantError(f'Unknown entity {entity_id}')
    return entry.unit_of_measurement