This repository has been archived by the owner on Nov 7, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
- Loading branch information
Showing
6 changed files
with
112 additions
and
32 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
79 changes: 61 additions & 18 deletions
79
packages/home-assistant-matter-hub/src/home-assistant-client/util/pattern-matcher.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,44 +1,87 @@ | ||
import globToRegExp from 'glob-to-regexp'; | ||
|
||
import { logger } from '@/logging/index.js'; | ||
import { HomeAssistantMatterEntity } from '@/models/index.js'; | ||
|
||
export interface PatternMatcherConfig { | ||
readonly includeDomains?: Array<string>; | ||
readonly includePatterns?: Array<string>; | ||
readonly excludeDomains?: Array<string>; | ||
readonly excludePatterns?: Array<string>; | ||
readonly includeDomains?: string[]; | ||
readonly excludeDomains?: string[]; | ||
|
||
readonly includePatterns?: string[]; | ||
readonly excludePatterns?: string[]; | ||
|
||
readonly includePlatforms?: string[]; | ||
readonly excludePlatforms?: string[]; | ||
|
||
readonly includeLabels?: string[]; | ||
readonly excludeLabels?: string[]; | ||
} | ||
|
||
export class PatternMatcher { | ||
private readonly log = logger.child({ service: 'PatternMatcher' }); | ||
|
||
private readonly includeDomains: Array<string>; | ||
private readonly includeDomains: string[]; | ||
private readonly excludeDomains: string[]; | ||
|
||
private readonly includePatterns: RegExp[]; | ||
private readonly excludeDomains: Array<string>; | ||
private readonly excludePatterns: RegExp[]; | ||
|
||
private readonly includePlatforms: string[]; | ||
private readonly excludePlatforms: string[]; | ||
|
||
private readonly includeLabels: string[]; | ||
private readonly excludeLabels: string[]; | ||
|
||
constructor(config: PatternMatcherConfig) { | ||
this.includeDomains = (config.includeDomains ?? []).map((domain) => `${domain}.`); | ||
this.includePatterns = config.includePatterns?.map((pattern) => globToRegExp(pattern)) ?? []; | ||
this.excludeDomains = (config.excludeDomains ?? []).map((domain) => `${domain}.`); | ||
|
||
this.includePatterns = config.includePatterns?.map((pattern) => globToRegExp(pattern)) ?? []; | ||
this.excludePatterns = config.excludePatterns?.map((pattern) => globToRegExp(pattern)) ?? []; | ||
|
||
this.includePlatforms = config.includePlatforms ?? []; | ||
this.excludePlatforms = config.excludePlatforms ?? []; | ||
|
||
this.includeLabels = config.includeLabels ?? []; | ||
this.excludeLabels = config.excludeLabels ?? []; | ||
} | ||
|
||
public isIncluded(entityId: string): boolean { | ||
const domainIncluded = this.includeDomains.some((domain) => entityId.startsWith(domain)); | ||
const patternIncluded = this.includePatterns.some((pattern) => pattern.test(entityId)); | ||
const included = | ||
this.includeDomains.length + this.includePatterns.length === 0 || domainIncluded || patternIncluded; | ||
const domainExcluded = this.excludeDomains.some((domain) => entityId.startsWith(domain)); | ||
const patternExcluded = this.excludePatterns.some((pattern) => pattern.test(entityId)); | ||
const excluded = domainExcluded || patternExcluded; | ||
public isIncluded(entity: HomeAssistantMatterEntity): boolean { | ||
const included = this.checkIncluded(entity); | ||
const excluded = this.checkExcluded(entity); | ||
if (excluded) { | ||
this.log.debug('%s is excluded', entityId); | ||
this.log.debug('%s is excluded', entity.entity_id); | ||
} else if (!included) { | ||
this.log.debug('%s is not included', entityId); | ||
this.log.debug('%s is not included', entity.entity_id); | ||
} else { | ||
this.log.debug('%s is included', entityId); | ||
this.log.debug('%s is included', entity.entity_id); | ||
} | ||
return included && !excluded; | ||
} | ||
|
||
private checkIncluded(entity: HomeAssistantMatterEntity): boolean { | ||
const domainIncluded = this.includeDomains.some((domain) => entity.entity_id.startsWith(domain)); | ||
const patternIncluded = this.includePatterns.some((pattern) => pattern.test(entity.entity_id)); | ||
const platformIncluded = this.includePlatforms.includes(entity.platform); | ||
const labelsIncluded = this.includeLabels.some((label) => entity.labels.includes(label)); | ||
return ( | ||
this.includeDomains.length + | ||
this.includePatterns.length + | ||
this.includePlatforms.length + | ||
this.includeLabels.length === | ||
0 || | ||
domainIncluded || | ||
patternIncluded || | ||
platformIncluded || | ||
labelsIncluded | ||
); | ||
} | ||
|
||
private checkExcluded(entity: HomeAssistantMatterEntity) { | ||
const domainExcluded = this.excludeDomains.some((domain) => entity.entity_id.startsWith(domain)); | ||
const patternExcluded = this.excludePatterns.some((pattern) => pattern.test(entity.entity_id)); | ||
const platformExcluded = this.excludePlatforms.includes(entity.platform); | ||
const labelsExcluded = this.excludeLabels.some((label) => entity.labels.includes(label)); | ||
return domainExcluded || patternExcluded || platformExcluded || labelsExcluded; | ||
} | ||
} |
19 changes: 16 additions & 3 deletions
19
packages/home-assistant-matter-hub/src/models/home-assistant-entity-registry-entry.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,26 @@ | ||
export interface HomeAssistantEntityRegistryEntry { | ||
entity_id: string; | ||
area_id?: string; | ||
categories: Record<string, unknown>; | ||
config_entry_id?: unknown; | ||
device_id?: string; | ||
disabled_by?: string; | ||
hidden_by?: string; | ||
disabled_by?: unknown; | ||
entity_category?: unknown; | ||
entity_id: string; | ||
has_entity_name: boolean; | ||
hidden_by?: unknown; | ||
icon?: unknown; | ||
id: string; | ||
labels?: string[]; | ||
name?: string; | ||
options?: { | ||
conversation?: { | ||
should_expose?: boolean; | ||
}; | ||
}; | ||
original_name: string; | ||
platform: string; | ||
translation_key?: unknown; | ||
unique_id: string; | ||
} | ||
|
||
export type HomeAssistantEntityRegistry = Record<string, HomeAssistantEntityRegistryEntry>; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters